In Python, we can loop through files in a directory easily with the listdir() function from the Python os module.

import os

path = r"examples"

for x in os.listdir(path):
    print(os.path.join(path,x))

#Ouput:
examplescode1.py
examplescode2.py

Note, the listdir() function returns a list of all names in a directory. To just get all files, you can check each name with the isdir() function.

import os

path = r"examples"

for x in os.listdir(path):
    if !os.path.isdir(os.path.join(path,x)):
        print(os.path.join(path,x))

#Output:
examplescode1.py
examplescode2.py

To loop over all files in a directory and and all subdirectories, you can use the os.walk() function.

import os

def getAllFiles(path):
    print(path)
    for root, dirs, files in os.walk(path):
        for name in files:
            print(os.path.join(root,name))
        for name in dirs:
            print(os.path.join(root,name))
            

getAllFiles(r"examples")

#Output:
examples
examplescode1.py
examplescode2.py
examplesexamples1
examplesexamples1code1_1.py
examplesexamples2
examplesexamples2code2_1.py
examplesexamples2code2_2.py
examplesexamples2code2_3.py

When working with file systems, it can be useful to be able to loop over all files in a particular directory.

The Python os module provides us with a number of great functions to be able to perform many operating system tasks.

With the os module, we can loop over all files in a particular directory easily.

The listdir() function takes in a path and gets a list of all of the files in that directory. We can then loop over that list to loop over the files in the directory.

Below is an example of how to loop over all files in a directory using Python.

import os

path = r"examples"

for x in os.listdir(path):
    print(os.path.join(path,x))

#Ouput:
examplescode1.py
examplescode2.py
examplesexamples1
examplesexamples2

listdir() returns all names in a directory. To just get the number of files, and ignore the subdirectories, you can check each name with the isdir() function.

import os

def getOnlyFiles(path):
    count = 0
    for x in os.listdir(path):
        if !os.path.isdir(os.path.join(path,x)):
            count = count + 1
    return count

print(getOnlyFiles(r"/examples"))

#Output:
examplescode1.py
examplescode2.py

Looping Over All Files in a Directories and Subdirectories in Python with os.walk()

Another great os module function is the os module walk() function. The walk() function returns the entire tree of folders and subfolders given a path.

We can use the walk() function to get all folders and subfolders, and then iterate over the returned object to count the number of files in each folder and subfolder.

Let’s say we have the following folder structure.

examples
-- code1.py
-- code2.py
-- examples1
---- code1_1.py
-- examples2
---- code2_1.py
---- code2_2.py
---- code2_3.py

In the 3 folders, we have a few files.

Let’s use the os walk() function to get the count of the files in each of the folders of our directory.

Below is the Python code which will allow you to get the number of files in each of the folders and subfolders of a given path.

import os

def getAllFiles(path):
    print(path)
    for root, dirs, files in os.walk(path):
        for name in files:
            print(os.path.join(root,name))
        for name in dirs:
            print(os.path.join(root,name))
            

getAllFiles(r"examples")

#Output:
examples
examplescode1.py
examplescode2.py
examplesexamples1
examplesexamples1code1_1.py
examplesexamples2
examplesexamples2code2_1.py
examplesexamples2code2_2.py
examplesexamples2code2_3.py

Looping Through Files in a Directory in Python Using pathlib Module

One last method we can use to loop through files in a directory using Python is to use the pathlib module.

We can utilize the pathlib.path().glob() function to get a list of all files of a certain extension easily.

Let’s say that we have the same folder structure from above.

We can get all of the ‘.py’ files easily with the pathlib.path().glob() function. We pass the path of our directory to path() and “**/*.py” to glob() to get all ‘.py’ files.

Below is an example of how to loop through the files in a directory using the Python pathlib module.

from pathlib import Path

def getAllPyFiles(path):
    pathlist = Path(path).glob('**/*.txt')
    for x in pathlist:
        print(x)

getAllPyFiles(r"examples")

#Output:
examplescode1.py
examplescode2.py
examplesexamples1code1_1.py
examplesexamples2code2_1.py
examplesexamples2code2_2.py
examplesexamples2code2_3.py

Hopefully this article has been useful for you to understand how to loop through files in a directory using Python.

Categorized in:

Python,

Last Update: March 15, 2024