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:
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 os
directory = 'files'
for filename in os.listdir(directory):
f = os.path.join(directory, filename)
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 os
directory = 'files'
for 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
from pathlib import Path
directory = 'files'
files = 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 os
directory = 'files'
for 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 glob
directory = 'files'
for filename in glob.iglob(f '{directory}/*' ):
print (filename)
|
Output:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
17 May, 2021
Like Article
Save Article