Sitemap

Using Python to Pull File Information

3 min readDec 29, 2023

--

Press enter or click to view image in full size
Photo by Hitesh Choudhary on Unsplash

Use Case

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. Create a script that generates a list of dictionaries about files in the working directory and also print the list of files.

Prerequisites

  1. Identify which integrated development environment (IDE) you will use for the Python code. I am going to use AWS Cloud9.
  2. Basic understanding of Python. Some helpful concepts include: lists, dictionary, and the os module.

This use case can be broken up into three main questions:

  1. How do I access the current working directory?
  2. How do I pull the file name and file size?
  3. How do I iterate through the current working directory to get this information?

Let’s get started.

How do I access the current working directory?

First, we want to import the os module. Refer to the Helpful References section toward the bottom of the article to learn more about the os module.

The os.getcwd() string will return the files we are seeking.

The os.listdir() string will return a list of all of the files in the current working directory.

We want to create an empty list that we will utilize later.

Get Madison Hersom’s stories in your inbox

Join Medium for free to get updates from this writer.

Copy and paste the below script into a file on the IDE. You will want to ensure the file has the “.py” extension. This will tell the system that it’s a Python file.

import os

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

# Generate file information
directory = os.listdir(cwd)

files_list = [] # Initialize an empty list to store file information

How do I pull the file name and file size?

We will continue using the os module to pull the file name and file size.

The os.path.basename() will provide us the file name.

The os.path.getsize will provide us the size of the file in bytes.

The file_info variable will pull our two key values (name and size) and format the returned response in an easy to read format.

By appending the list, we will be able to store the list information generated from our requests. We created an empty list previously to prepare for these additions.

Copy and paste the below script into the same file in your IDE.

# How to get the file name and size.
def get_file_info(file_path):
file_name = os.path.basename(file_path)
file_size = os.path.getsize(file_path)
file_info = {'File Name: ': file_name, 'File Size: ': file_size}
files_list.append(file_info)
print(files_list)

How do I iterate through the current working directory to get this information?

We will want to use a “for loop” to iterate through the file directory. For loops are perfect for iterating through lists.

# Iterate through each file in the directory
for file_name in directory:
file_path = os.path.join(cwd, file_name)
get_file_info(file_path)

Lastly, we need to print our list of files and file sizes.

# Print the list of dictionaries
for file_info in files_list:
print(file_info)

Copy and paste the above items into the same file in your IDE. Save the file.

Conclusion

Now, if you put the Python code altogether, you’ll create a script that will generate a list of dictionaries about files in the working directory, provide file information (e.g. name and size), and print the list of the files.

Helpful References

--

--