How do I read all files in a directory in Python?

Getting a list of all files in a directory and its subdirectories can be quite a common task, so, in this tutorial I will show you how you can do this with 4 lines of code using os.walk.

Below you can see how we can recursively loop through all the files in a given directory:

import os for path, currentDirectory, files in os.walk("/Users/darren/Desktop/test"): for file in files: print(os.path.join(path, file))

In the above code I join the path and the file to give me the full path to each file that exists within the directory that I provided.

When I run the above code I have the following output:

/Users/darren/Desktop/test/.DS_Store /Users/darren/Desktop/test/file 1.rtf /Users/darren/Desktop/test/test 1 level/.DS_Store /Users/darren/Desktop/test/test 1 level/file 2.rtf

There are a few other ways to do this, but I prefer to use os.walk because of how simple it is to use.

Need to list all text files in a directory using Python?

If so, you may use the following templates to list your files:

List all the text files in a directory:

import glob import os os.chdir(r'directory where the files are located') my_files = glob.glob('*.txt') print(my_files)

List the paths of the text files:

import glob my_files_path = glob.glob(r'directory where the files are located\*.txt') print(my_files_path)

In the next section, you’ll see an example with the steps to list all text files using Python.

Steps to List all Text Files in a Directory using Python

Step 1: Locate the directory that contains the text files

For example, let’s suppose that the following 2 text files are stored in a folder called Test:

New Products
Old Products

Step 2: Capture the path where the text files are stored

Next, capture the path of the directory where the text files are stored.

For our example, the path where the 2 files are stored is as follows:

C:\Users\Ron\Desktop\Test

You’ll need to modify the path to reflect the location where the text files are stored on your computer.

Step 3: List all text files in a directory using Python

To list all the text files in a directory using Python, you’ll need to import the glob and os packages.

You can then use the following template to list your text files:

import glob import os os.chdir(r'directory where the files are located') my_files = glob.glob('*.txt') print(my_files)

And for our example, this is the complete Python code to list the text files:

import glob import os os.chdir(r'C:\Users\Ron\Desktop\Test') my_files = glob.glob('*.txt') print(my_files)

Run the code (adjusted to your path) and you’ll see the list of the text files:

['New Products.txt', 'Old Products.txt']

Don’t forget to place “r” before the path to avoid the following error in Python:

(unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape

Optional Step: List the paths of the text files

What if you want to get a list of the paths of your text files?

If that’s the case, you may use the following template:

import glob my_files_path = glob.glob(r'directory where the files are located\*.txt') print(my_files_path)

And for our example:

import glob my_files_path = glob.glob(r'C:\Users\Ron\Desktop\Test\*.txt') print(my_files_path)

These are the paths for our example:

['C:\\Users\\Ron\\Desktop\\Test\\New Products.txt', 'C:\\Users\\Ron\\Desktop\\Test\\Old Products.txt']

Many careers in tech pay over $100,000 per year. With help from Career Karma, you can find a training program that meets your needs and will set you up for a long-term, well-paid career in tech.

By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

In this article, we will see how to list all files of a directory in Python. There are multiple ways to list files of a directory. In this article, We will use the following four methods.

  • os.listdir('dir_path'): Return the list of files and directories present in a specified directory path.
  • os.walk('dir_path'): Recursively get the list all files in directory and subdirectories.
  • os.scandir('path'): Returns directory entries along with file attribute information.
  • glob.glob('pattern'): glob module to list files and folders whose names follow a specific pattern.

How to List All Files of a Directory

Getting a list of files of a directory is easy as pie! Use the listdir() and isfile() functions of an os module to list all files of a directory. Here are the steps.

  1. Import os module

    This module helps us to work with operating system-dependent functionality in Python. The os module provides functions for interacting with the operating system.

  2. Use os.listdir() function

    The os.listdir('path') function returns a list containing the names of the files and directories present in the directory given by the path.

  3. Iterate the result

    Use for loop to Iterate the files returned by the listdir() function. Using for loop we will iterate each file returned by the listdir() function

  4. Use isfile() function

    In each loop iteration, use the os.path.isfile('path') function to check whether the current path is a file or directory. If it is a file, then add it to a list. This function returns True if a given path is a file. Otherwise, it returns False.

Example to List Files of a Directory

Let’s see how to list files of an ‘account’ folder. The listdir() will list files only in the current directory and ignore the subdirectories.

Example 1: List only files from a directory

import os # folder path dir_path = r'E:\\account\\' # list to store files res = [] # Iterate directory for path in os.listdir(dir_path): # check if current path is a file if os.path.isfile(os.path.join(dir_path, path)): res.append(path) print(res)

Output:

Here we got three file names.

['profit.txt', 'sales.txt', 'sample.txt']

If you know generator expression, you can make code smaller and simplers using a generator function as shown below.

Generator Expression:

import os def get_files(path): for file in os.listdir(path): if os.path.isfile(os.path.join(path, file)): yield file

Then simply call it whenever required.

for file in get_files(r'E:\\account\\'): print(file)

Example 2: List both files and directories.

Directly call the listdir('path') function to get the content of a directory.

import os # folder path dir_path = r'E:\\account\\' # list file and directories res = os.listdir(dir_path) print(res)

Output:

As you can see in the output, ‘reports_2021’ is a directory.

['profit.txt', 'reports_2021', 'sales.txt', 'sample.txt']

The os.walk() function returns a generator that creates a tuple of values (current_path, directories in current_path, files in current_path).

Note: Using the os.walk() function we can list all directories, subdirectories, and files in a given directory.

It is a recursive function, i.e., every time the generator is called, it will follow each directory recursively to get a list of files and directories until no further sub-directories are available from the initial directory.

For example, calling the os.walk('path') will yield two lists for each directory it visits. The first list contains files, and the second list includes directories.

Let’s see the example to list all files in directory and subdirectories.

Example:

from os import walk # folder path dir_path = r'E:\\account\\' # list to store files name res = [] for (dir_path, dir_names, file_names) in walk(dir_path): res.extend(file_names) print(res)

Output:

['profit.txt', 'sales.txt', 'sample.txt', 'december_2021.txt']

Note: Add break inside a loop to stop looking for files recursively inside subdirectories.

Example:

from os import walk # folder path dir_path = r'E:\\account\\' res = [] for (dir_path, dir_names, file_names) in walk(dir_path): res.extend(file_names) # don't look inside any subdirectory break print(res)

os.scandir() to get files of a directory

The scandir() function returns directory entries along with file attribute information, giving better performance for many common use cases.

It returns an iterator of os.DirEntry objects, which contains file names.

Example:

import os # get all files inside a specific folder dir_path = r'E:\\account\\' for path in os.scandir(dir_path): if path.is_file(): print(path.name)

Output:

profit.txt sales.txt sample.txt

Glob Module to list Files of a Directory

The Python glob module, part of the Python Standard Library, is used to find the files and folders whose names follow a specific pattern.

For example, to get all files of a directory, we will use the dire_path/*.* pattern. Here, *.* means file with any extension.

Read more: Python list files in a directory with extension txt.

Let’s see how to list files from a directory using a glob module.

Example:

import glob # search all files inside a specific folder # *.* means file name with any extension dir_path = r'E:\account\*.*' res = glob.glob(dir_path) print(res)

Output:

['E:\\account\\profit.txt', 'E:\\account\\sales.txt', 'E:\\account\\sample.txt']

Note: If you want to list files from subdirectories, then set the recursive attribute to True.

Example:

import glob # search all files inside a specific folder # *.* means file name with any extension dir_path = r'E:\demos\files_demos\account\**\*.*' for file in glob.glob(dir_path, recursive=True): print(file)

Output:

E:\account\profit.txt E:\account\sales.txt E:\account\sample.txt E:\account\reports_2021\december_2021.txt

Pathlib Module to list files of a directory

From Python 3.4 onwards, we can use the pathlib module, which provides a wrapper for most OS functions.

  • Import pathlib module: Pathlib module offers classes and methods to handle filesystem paths and get data related to files for different operating systems.
  • Next, Use the pathlib.Path('path') to construct directory path
  • Next, Use the iterdir() to iterate all entries of a directory
  • In the end, check if a current entry is a file using the path.isfile() function

Example:

import pathlib # folder path dir_path = r'E:\\account\\' # to store file names res = [] # construct path object d = pathlib.Path(dir_path) # iterate directory for entry in d.iterdir(): # check if it a file if entry.is_file(): res.append(entry) print(res)

Tải thêm tài liệu liên quan đến bài viết How do I read all files in a directory in Python?