Python Loop through Folders and Files in Directory
Last Updated :
19 Sep, 2024
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.
Below are the ways by which we can iterate over files in a directory in Python:
Iterate Over Files 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.
Python
# 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:
ListdirPython to Loop Through Files 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.
Python
# 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:
WalkIterate Over Files 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.
Python
# 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:
ScandirPython to Loop Through Files 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.
Python
# 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:
GlobUsing 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.
Python
# 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
Similar Reads
Delete a directory or file using Python In this article, we will cover how to delete (remove) files and directories in Python. Python provides different methods and functions for removing files and directories. One can remove the file according to their need.Table of ContentUsing the os.remove() MethodDelete a FileRemove file with absolut
6 min read
Filenotfounderror: Errno 2 No Such File Or Directory in Python When working with file operations in programming, encountering errors is not uncommon. One such error that developers often come across is the FileNotFoundError with the Errno 2: No such file or directory message. This error indicates that the specified file or directory could not be found at the gi
3 min read
Check if a File Exists in Python When working with files in Python, we often need to check if a file exists before performing any operations like reading or writing. by using some simple methods we can check if a file exists in Python without tackling any error. Using pathlib.Path.exists (Recommended Method)Starting with Python 3.4
3 min read
Create a File Path with Variables in Python The task is to create a file path using variables in Python. Different methods we can use are string concatenation and os.path.join(), both of which allow us to build file paths dynamically and ensure compatibility across different platforms. For example, if you have a folder named Documents and a f
3 min read
Os.Listdir() is not Working in Python The os.listdir function serves as a fundamental tool for retrieving the contents of a directory. However, users occasionally encounter perplexing situations where the function fails to display all the expected files. This article delves into the intricacies of this phenomenon, exploring the reasons
3 min read
Python Program to Get the File Name From the File Path In this article, we will be looking at the program to get the file name from the given file path in the Python programming language. Sometimes during automation, we might need the file name extracted from the file path. Better to have knowledge of:Python OS-modulePython path moduleRegular expression
5 min read
How to use OS with Python List Comprehension One such module is the 'os' module, which allows interaction with the operating system. When combined with list comprehensions, the 'os' module becomes a potent tool for handling file and directory operations concisely and expressively. In this article, we will see how we can use the os module with
3 min read
Python | Sort and store files with same extension Have you ever wanted to find any particular file in a folder, but then completely freak out when you find that folder to be a hell of a mess? Well, Python is a rescue here. Using Python OS-module and shutil module, we can organize the files with same extensions and store in separate folders. Look at
2 min read
How Use Linux Command In Python Using System.Os Using the system module from the os library in Python allows you to interact with the Linux command line directly from your Python script. This module provides a way to execute system commands, enabling you to automate various tasks by integrating Linux commands seamlessly into your Python code. Whe
3 min read