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
Create a watchdog in Python to look for filesystem changes
Many times a file is needed to be processed at the time of its creation or its modification. This can be done by following changes in a particular directory. There are many ways in python to follow changes made in a directory. One such way is to use the watchdog module. As the name suggests this mod
4 min read
Finding Md5 of Files Recursively in Directory in Python
MD5 stands for Message Digest Algorithm 5, it is a cryptographic hash function that takes input(or message) of any length and produces its 128-bit(16-byte) hash value which is represented as a 32-character hexadecimal number. The MD5 of a file is the MD5 hash value computed from the content of that
3 min read
Rename all file names in your directory using Python
Given multiple files in a directory having different names, the task is to rename all those files in sorted order. We can use OS module in order to do this operation. The OS module in Python provides functions for interacting with the operating system and provides a portable way of using operating s
1 min read
Copy Contents of One File to Another File - Python
Given two text files, the task is to write a Python program to copy the contents of the first file into the second file. The text files which are going to be used are first.txt and second.txt:Using File handling to read and appendWe will open first.txt in 'r' mode and will read the contents of first
2 min read
How To Detect File Changes Using Python
In the digital age, monitoring file changes is essential for various applications, ranging from data synchronization to security. Python offers robust libraries and methods to detect file modifications efficiently. In this article, we will see some generally used method which is used to detect chang
3 min read
Check If a Text File Empty in Python
Before performing any operations on your required file, you may need to check whether a file is empty or has any data inside it. An empty file is one that contains no data and has a size of zero bytes. In this article, we will look at how to check whether a text file is empty using Python.Check if a
4 min read
Implementing a Contacts directory in Python
Our task is to implement a smartphone directory that collects contact data from the user until the user prompts the program to. Contact data refers to the contact's name, phone number, date-of-birth, a category that contact belongs to (Friends, Family, Work, Other), e-mail address. The user may ente
12 min read
How Can I Make One Python File Run Another File?
In Python programming, there often arises the need to execute one Python file from within another. This could be for modularity, reusability, or simply for the sake of organization. In this article, we will explore different approaches to achieve this task, each with its advantages and use cases. Ma
2 min read