Open In App

Python Loop through Folders and Files in Directory

Last Updated : 22 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

File iteration is a crucial process of working with files in Python. The process of accessing and processing each item in any collection is called File iteration in Python, which involves looping through a folder and perform operation on each file. In this article, we will see how we can iterate over files in a directory in Python.

Python Loop through Folders and Files in Directory

Below are the ways by which we can iterate over files in a directory in Python:

  1. Using os.listdir() method
  2. Using os.walk() method
  3. Using os.scandir() method
  4. Using glob module
  5. Using pathlib module

Using os.listdir() method

In this example, the Python script utilizes the ‘os’ module to list and iterate through files in the specified directory, which is assigned to the variable ‘directory.’ For each file, it opens and reads its content, printing both the file name and its content to the console. The ‘os.path.join’ function is employed to ensure the correct path is used when accessing each file.

Python3




# Import module
import os
 
# Assign directory
directory = r"C:\Users\Lenovo\Downloads\gfg-test"
 
# Iterate over files in directory
for name in os.listdir(directory):
    # Open file
    with open(os.path.join(directory, name)) as f:
        print(f"Content of '{name}'")
        # Read content of file
        print(f.read())
 
    print()


Output:

listdir

Listdir

Using os.walk() method

In this example, the Python script employs the ‘os’ module and ‘os.walk’ function to recursively traverse through the specified directory and its subdirectories. For each file encountered, it opens and prints the content along with the file name. Additionally, for each subdirectory, it lists and prints the contents of the folder, providing a comprehensive overview of both files and subfolders within the specified directory. The use of ‘os.path.join‘ ensures correct path construction for accessing files. The ‘break’ statement is utilized to limit the output to the first level of the directory structure.

Python3




# Import module
import os
 
# Assign directory
directory = r"C:\Users\Lenovo\Downloads\gfg-test"
 
# Iterate over files in directory
for path, folders, files in os.walk(directory):
    # Open file
    for filename in files:
        with open(os.path.join(directory, filename)) as f:
            print(f"Content of '{filename}'")
            # Read content of file
            print(f.read())
        print()
 
    # List contain of folder
    for folder_name in folders:
        print(f"Content of '{folder_name}'")
        # List content from folder
        print(os.listdir(f"{path}/{folder_name}"))
        print()
 
    break


Output:

walk

Walk

Using os.scandir() method

In this example, the Python script utilizes the ‘os’ module and os.scandir() function to iterate through files in the specified directory. For each file encountered, it opens and prints both the file name and its content to the console, employing ‘os.path.join’ to ensure the correct path is used. This approach is concise and efficient for iterating through files in a directory without the need for recursive traversal or explicit handling of subdirectories.

Python3




# Import module
import os
 
# Assign directory
directory = r"C:\Users\Lenovo\Downloads\gfg-test"
 
# Iterate over files in directory
for filename in os.scandir(directory):
    # Open file
    with open(os.path.join(directory, filename)) as f:
        print(f"Content of '{filename}'")
        # Read content of file
        print(f.read())
    print()


Output:

scandir

Scandir

Using glob module

In this example, the Python script utilizes the glob module and ‘glob.glob’ function to iterate through files in the specified directory. For each file encountered, it opens and prints both the file name and its content to the console, using ‘os.path.join’ to ensure the correct path is used. This approach simplifies the file iteration process and provides flexibility in specifying file patterns, making it a concise method for processing files within a given directory.

Python3




# Import module
import glob
import os
 
# Assign directory
directory = r"C:\Users\Lenovo\Downloads\gfg-test"
 
# Iterate over files in directory
for filename in glob.glob(f"{directory}/*"):
    # Open file
    with open(os.path.join(directory, filename)) as f:
        print(f"Content of '{filename}'")
        # Read content of file
        print(f.read())
    print()


Output:

pathlib

Glob

Using pathlib module

In this example, the Python script utilizes the ‘os’ module along with the ‘pathlib‘ module to iterate through files in the specified directory. The ‘Path’ class is used to create a Path object representing the directory, and the ‘glob’ method is employed to find all files within it. The script then iterates over the files, opens each file, and prints both the file name and its content to the console. The use of ‘os.path.join’ ensures correct path construction for accessing files.

Python3




# Import module
import os
from pathlib import Path
 
# Assign directory
directory = r"C:\Users\Lenovo\Downloads\gfg-test"
 
# Find all files from directory
files = Path(directory).glob("*")
# Iterate over files in directory
for filename in files:
    # Open file
    with open(os.path.join(directory, filename)) as f:
        print(f"Content of '{filename}'")
        # Read content of file
        print(f.read())
    print()


Output:

pathlib

Pathlib



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads