How to iterate over files in directory using Python?
Last Updated :
24 Feb, 2025
Iterating over files in a directory using Python involves accessing and processing files within a specified folder. Python provides multiple methods to achieve this, depending on efficiency and ease of use. These methods allow listing files, filtering specific types and handling subdirectories.
Using os.scandir()
os.scandir() provides an iterator of os.DirEntry objects, making it the fastest way to iterate over files in a directory. It allows direct access to file attributes like .is_file(), avoiding extra system calls. This makes it more memory-efficient and faster than os.listdir(), especially for large directories.
Python
import os # import os module
directory = 'files' # set directory path
for entry in os.scandir(directory):
if entry.is_file(): # check if it's a file
print(entry.path)
Output:

Explanation: loop iterates through each entry in the directory and entry.is_file() checks if it is a file not a folder . If true, it prints the file’s full path.
Using pathlib.Path().iterdir()
pathlib module offers an object-oriented approach to handling file paths. Using Path(directory).iterdir(), we can efficiently iterate over all files in a directory. It is a more modern alternative to os methods and provides an intuitive way to work with file paths in Python.
Python
from pathlib import Path # import Path from pathlib module
directory = Path('files') # set directory path
for file in directory.iterdir():
if file.is_file(): # Check if it's a file
print(file)
Output:

Explanation: It imports Path, which offers an object-oriented way to handle file paths. Path('files') sets the directory and iterdir() loops through its entries. file.is_file() checks if an entry is a file and if true, its path is printed.
Using os.walk()
os.walk() is useful when recursively iterating over all files in a directory and its subdirectories. It returns a 3-tuple containing the root directory, subdirectories and filenames, making it ideal for deep directory traversal. However, it may be slightly slower for flat directories.
Python
import os # import os module
directory = 'files' # set directory path
for root, _, files in os.walk(directory):
for filename in files: # loop through files in the current directory
print(os.path.join(root, filename))
Output:

Explanation: This code iterates over files in 'files' using os.walk(), which traverses directories recursively. It loops through files in each folder and prints their full paths.
Using glob.iglob()
glob module is useful for pattern-based file matching while iterating through a directory. glob.iglob() returns an iterator instead of a list, making it more memory-efficient. This method is ideal when working with specific file extensions (e.g., *.txt or *.csv).
Python
import glob # import glob module
directory = 'files' # set directory path
for filename in glob.iglob(f'{directory}/*'):
print(filename)
Output:

Explanation: This code iterates over files in 'files' using glob.iglob(), which matches files based on a pattern. It prints the full path of each file.
Similar Reads
How to print all files within a directory using Python? The OS module is one of the most popular Python modules for automating the systems calls and operations of an operating system. With a rich set of methods and an easy-to-use API, the OS module is one of the standard packages and comes pre-installed with Python. In this article, we will learn how to
3 min read
List all files of certain type in a directory using Python In python, there are several built-in modules and methods for file handling. These functions are present in different modules such as os, glob, etc. This article helps you find out many of the functions in one place which gives you a brief knowledge about how to list all the files of a certain type
3 min read
How to move all files from one directory to another using Python ? In this article, we will see how to move all files from one directory to another directory using Python. Â In our day-to-day computer usage we generally copy or move files from one folder to other, now let's see how to move a file in Python: This can be done in two ways:Using os module.Using shutil m
2 min read
How to List all Files and Directories in FTP Server using Python? FTP ( File Transfer Protocol ) is set of rules that computer follows to transfer files across computer network. It is TCP/IP based protocol. FTP lets clients share files. FTP is less secure because of files are shared as plain text without any encryption across the network. It is possible using pyt
2 min read
Python - Get list of files in directory with size In this article, we are going to see how to extract the list of files of the directory along with its size. For this, we will use the OS module. OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a
9 min read
How to open multiple files using "with open" in Python? Python has various highly useful methods for working with file-like objects, like the with feature. But what if we need to open several files in this manner? You wouldn't exactly be "clean" if you had a number of nested open statements. In this article, we will demonstrate how to open multiple files
2 min read