How to List Files in a Directory with Python?

As a data scientist working in New York City, I often need to analyze large numbers of data files stored in various folders on my computer. In this tutorial, I will explain how to list files in a directory with Python with suitable examples and screenshots.

List Files in a Directory with Python

There are a few different ways to list files in a directory using Python. The most common methods are explained below.

Read How to Save a Python Dictionary as a JSON File?

1. Use os.listdir() Method

The simplest way to get a list of all files in a directory is using the os.listdir() function from the Python built-in module os in as explained here. Here’s an example:

import os

# Specify the directory 
dir_path = r"C:\Users\Public\code"

# Get list of files
files = os.listdir(dir_path)

# Print the list of files
for file in files:
    print(file)

Output:

austin_texas.txt
calculator.py
chicago_sales.txt
config.txt

I executed the above example code and added the screenshot below.

List Files in a Directory with Python

This will print out the names of all files and subdirectories in the “code” folder for the user “Public” on Windows.

Check out How to Write a Variable to a File in Python?

2. Use os.walk() Method

To get a list of all files within a directory and its subdirectories, use the Python os.walk() function as shown in this guide. It will recursively walk through all subdirectories. Here’s an example:

import os

# Set the directory you want to start from
root_dir = '.'

for dir_path, subdirs, files in os.walk(root_dir):
    for file in files:
        print(os.path.join(dir_path, file))

I executed the above example code and added the screenshot below.

How to List Files in a Directory with Python

This will print the full path of every file in the current directory and all its subdirectories.

Read Python Create File

3. Use glob Module

If you only want to list files matching a certain pattern, like all CSV files for example, the glob module provides an easy way to do that as covered in this tutorial. Here’s an example:

import glob

csv_files = glob.glob('*.csv')

print(csv_files)

Output:

['customers.csv', 'large_sales_data.csv', 'people.csv', 'people_pandas.csv', 'sales_data.csv', 'sales_data_20.csv', 'sales_data_21.csv', 'sales_report.csv']

I executed the above example code and added the screenshot below.

List Files in a Directory with Python glob module

This will print a list of all files with the .csv extension in the current directory.

Check out Python Get File Extension

Automate File Processing Tasks

Once you have a list of the relevant files, you can easily automate processing and analysis tasks as described in this article on Python automation ideas. For example, you could loop through the list of CSV files, read each one into a Pandas DataFrame, perform some data cleaning and aggregation, and generate a summary report. The possibilities are endless!

I hope this tutorial has helped explain the different ways to list files in a directory using Python and given you some ideas for how to automate your file-processing workflows. With these techniques, wrangling large collections of data files becomes a breeze.

Read Python Get Filename From Path

Conclusion

In this tutorial, I have explained how to list files in a directory with Python. I covered various methods to accomplish this task such as using the os.listdir() method, using os.walk() method, and using glob module. I also discussed how to automate file-processing tasks.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.