Sitemap
Brandon White

Brandon White

Extracting File Data Using Python

5 min readSep 4, 2024
Press enter or click to view image in full size

Scenario: Your company needs to learn about the files located on various machines. You have been asked to build a script that extracts information such as the name and size about the files in the current working directory and stores it in a list of dictionaries.

Task: Create a script that generates a list of dictionaries about files in the working directory. Then print the list. Push code to GitHub.

Code Guide:

import os

This is used to import the built-in OS module, which provides a way of using operating system-dependent functionality. It allows showing contents of directories and getting file sizes etc.

# Get the current working directory
current_directory = os.getcwd()

The next line gets the path of the directory where the script is currently running and stores it in the variable “current directory”. It allows the script to know and to use the current directory for future operations, such as listing files there.

# List all files in the current working directory
file_list = os.listdir(current_directory)

This collects a list of the names of files and directories located in the directory specified by “current directory” and stores this list in the variable “file list”. Now, this list can then be used for future operations on each file or directory in the specified directory.

# Initialize an empty list to hold the file information
file_info_list = []

This command initializes an empty list and assigns it to the variable “file info list". This list is to store dictionaries containing information about each file (name and size for example) as the script processes them in the current working directory.

# Loop through the files and gather their information
for file_name in file_list:
# Get the full path to the file
full_path = os.path.join(current_directory, file_name)

The for-loop is to iterate over each file name in the list of files, constructing the full path for each file, and storing that full path in the variable “full path” for future use.

 # Check if it is a file (not a directory)
if os.path.isfile(full_path):
# Get the size of the file in bytes
file_size = os.path.getsize(full_path)

This command checks that the “full path" corresponds to a regular file; the script then retrieves the size of said file in bytes and stores its size in the variable “file size". This is gathering information about each file in the current working directory.

# Create a dictionary with the file's name and size
file_info = {
"file_name": file_name,
"file_size": file_size # File size in bytes
}

Pretty explanatory here. This creates a dictionary to store the name and size of the file.

 # Add the dictionary to the list
file_info_list.append(file_info)

This essentially ensures that the script will accumulate a list of dictionaries, each representing a file in the current working directory. It adds the “file info” dictionary to the end of the “file info list”.

# Print the list of file information
print(file_info_list)

The final line is to see all of the collected information about the files in the working directory. Each entry on the list printed, represents a dictionary with details about an individual file.

Get Brandon White’s stories in your inbox

Join Medium for free to get updates from this writer.

The Code in Full

import os

# Get the current working directory
current_directory = os.getcwd()

# List all files in the current working directory
file_list = os.listdir(current_directory)

# Initialize an empty list to hold the file information
file_info_list = []

# Loop through the files and gather their information
for file_name in file_list:
# Get the full path to the file
full_path = os.path.join(current_directory, file_name)

# Check if it is a file (not a directory)
if os.path.isfile(full_path):
# Get the size of the file in bytes
file_size = os.path.getsize(full_path)

# Create a dictionary with the file's name and size
file_info = {
"file_name": file_name,
"file_size": file_size # File size in bytes
}

# Add the dictionary to the list
file_info_list.append(file_info)

# Print the list of file information
print(file_info_list)
Press enter or click to view image in full size
The script being ran in Cloud9 (AWS)

File Name and Size

Press enter or click to view image in full size

Pushing Script to GitHub through AWS Cloud9

To ensure my script is saved and can be accessed through a GitHub repository, I will need to “push” it through my command line via Cloud9.

Using the “git clone’ command plus my repository URL, I have access through Cloud9…

Press enter or click to view image in full size

I also want to make sure I’m in the correct directory before I push my code…

First I stage the change by clicking the source control tab, and the plus symbol which will prompt me to enter a message to complete the stage change.

Once this is completed, at the bottom left there will be an upward pointing arrow indicating that your code is ready to be pushed…press the arrow and if everything is correct; the code should be added to the repository on GitHub.

Press enter or click to view image in full size

GitHub repository link below

https://github.com/BWhite487/DataExtraction

--

--