Create a script that generates a list of dictionaries about files in the working directory.
Python is a powerful and flexible general-purpose language with many applications. Python 3 is the latest version of the language, and it’s great for new and seasoned developers alike. In fact, it’s one of the most popular programming languages in the world
Overview
Welcome to this step-by-step tutorial on creating a Python script to extract essential information about files in the working directory of various machines. In this tutorial, we will walk through the process of building a powerful script that captures file names and sizes and stores them in a list of dictionaries. Get ready to embark on this exciting journey of mastering Python and empowering your company with a valuable file information extraction tool.
Objectives:
- Create a script that generates a list of dictionaries about files in the working directory. Then print the list.
Prerequisite:
- Basic knowledge of Python programming.
- Python installed on your machine.

Step 1: Setting up the Project
- Create a new folder for your project. This folder will contain your script and other related files.

2. Navigate to the directory we’ve just created.

Next we will create a prep file:

- Create a script that generates random binary files using the
random
,uuid
, andos
modules. - Start by importing the necessary modules:
import random
import uuid
import os
3. Creating the generate_file
Function:
- Define the
generate_file
function. This function will take two optional parameters:name
(file name) andsize
(file size in bytes).
def generate_file(name: str = None, size: int = random.randint(1, 10)) -> None:
if name is None:
name = str(uuid.uuid4())[:8] + '.dummy'
with open(name, 'wb') as fout:
fout.write(os.urandom(size))
fout.close()
4. Generating Random Files:
- To generate random files, include the following block of code within the
if __name__ == '__main__':
block.
if __name__ == '__main__':
for i in range(10):
generate_file()
Let’s take a look at the full script:

5. Running the Script:
- Save the script with a descriptive name, such as
prep.py
. - Open your terminal or command prompt and navigate to the directory where you saved the script.
- Run the script using the following command:
python3 prep.py

Step 2: Writing the Script
- Create a new Python file within your project folder. You can name it something like
extract_file_info.py
.

- Open the file in your chosen text editor or IDE.
- Start by importing the necessary modules:
import os
Define a function that extracts information about files in the current working directory and stores it in a list of dictionaries:
import os
def extract_file_info():
file_list = [] # Initialize an empty list to store file information
for filename in os.listdir(): # Iterate through items in the current directory
if os.path.isfile(filename): # Check if the item is a file
file_info = {
'name': filename, # Create a dictionary entry for the file name
'size': os.path.getsize(filename) # Add the file size using os.path.getsize()
}
file_list.append(file_info) # Append the file dictionary to the file_list
return file_list # Return the list containing file information dictionaries
if __name__ == '__main__':
file_info_list = extract_file_info() # Call the function to extract file information
for file_info in file_info_list: # Iterate through the list of file information dictionaries
print(file_info) # Print each dictionary containing file name and size
Call the extract_file_info
function and print the resulting list of dictionaries:
if __name__ == '__main__':
file_info_list = extract_file_info()
for file_info in file_info_list:
print(file_info)
Step 3: Testing the Script
- Open a terminal or command prompt.
- Navigate to the project folder using the
cd
command.

- Run the script using the following command:
python3 extract_file_info.py

You should see a list of dictionaries printed in the terminal, each containing file information.

Success!!!!
Conclusion!
Congratulations! In this tutorial we have successfully built a Python script capable of extracting file information from the current working directory on various machines. Hopefully this tutorial was fun and informative. Thank you for following along, happy coding!