Python Loop through Folders and Files in Directory
Last Updated :
22 Feb, 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.
Python Loop through Folders and Files in Directory
Below are the ways by which we can iterate over files in a directory in Python:
- Using os.listdir() method
- Using os.walk() method
- Using os.scandir() method
- Using glob module
- 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 os
directory = r "C:\Users\Lenovo\Downloads\gfg-test"
for name in os.listdir(directory):
with open (os.path.join(directory, name)) as f:
print (f "Content of '{name}'" )
print (f.read())
print ()
|
Output:
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 os
directory = r "C:\Users\Lenovo\Downloads\gfg-test"
for path, folders, files in os.walk(directory):
for filename in files:
with open (os.path.join(directory, filename)) as f:
print (f "Content of '{filename}'" )
print (f.read())
print ()
for folder_name in folders:
print (f "Content of '{folder_name}'" )
print (os.listdir(f "{path}/{folder_name}" ))
print ()
break
|
Output:
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 os
directory = r "C:\Users\Lenovo\Downloads\gfg-test"
for filename in os.scandir(directory):
with open (os.path.join(directory, filename)) as f:
print (f "Content of '{filename}'" )
print (f.read())
print ()
|
Output:
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 glob
import os
directory = r "C:\Users\Lenovo\Downloads\gfg-test"
for filename in glob.glob(f "{directory}/*" ):
with open (os.path.join(directory, filename)) as f:
print (f "Content of '{filename}'" )
print (f.read())
print ()
|
Output:
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 os
from pathlib import Path
directory = r "C:\Users\Lenovo\Downloads\gfg-test"
files = Path(directory).glob( "*" )
for filename in files:
with open (os.path.join(directory, filename)) as f:
print (f "Content of '{filename}'" )
print (f.read())
print ()
|
Output:
Pathlib
Share your thoughts in the comments
Please Login to comment...