How to iterate over files in directory using Python?
Directory also sometimes known as a folder are unit organizational structure in a system’s file system for storing and locating files or more folders. Python as a scripting language provides various methods to iterate over files in a directory.
Below are the various approaches by using which one can iterate over files in a directory using python:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Method 1: os.listdir()
This function returns the list of files and subdirectories present in the given directory. We can filter the list to get only the files using os.path.isfile() function:
Example:
Python3
# import required moduleimport os# assign directorydirectory = 'files'# iterate over files in# that directoryfor filename in os.listdir(directory): f = os.path.join(directory, filename) # checking if it is a file if os.path.isfile(f): print(f) |
Output:

Method 2: os.scandir()
This method is used to get an iterator of os.DirEntry objects corresponding to the entries in the directory given by specified path.
Example:
Python3
# import required moduleimport os# assign directorydirectory = 'files'# iterate over files in# that directoryfor filename in os.scandir(directory): if filename.is_file(): print(filename.path) |
Output:

Method 3: pathlib module
We can iterate over files in a directory using Path.glob() function which glob the specified pattern in the given directory and yields the matching files. Path.glob(‘*’) yield all the files in the given directory
Example:
Python3
# import required modulefrom pathlib import Path# assign directorydirectory = 'files'# iterate over files in# that directoryfiles = Path(directory).glob('*')for file in files: print(file) |
Output:

Method 4: os.walk()
We can also search for subdirectories using this method as it yields a 3-tuple (dirpath, dirnames, filenames).
- root: Prints out directories only from what you specified.
- dirs: Prints out sub-directories from the root.
- files: Prints out all files from root and directories.
Python3
# import required moduleimport os# assign directorydirectory = 'files'# iterate over files in# that directoryfor root, dirs, files in os.walk(directory): for filename in files: print(os.path.join(root, filename)) |
Output:

Method 5: glob module
The glob.iglob() function returns an iterator over the list of pathnames that match the given pattern.
Example:
Python3
# import required moduleimport glob# assign directorydirectory = 'files'# iterate over files in# that directoryfor filename in glob.iglob(f'{directory}/*'): print(filename) |
Output:


