Python – Loop through files of certain extensions

10 Jul, 2022
A directory is capable of storing multiple files and python can support a mechanism to loop over them. In this article, we will see different methods to iterate over certain files in a given directory or subdirectory.
Path containing different files: This will be used for all methods.
Method 1: Using listdir()
In this method, we will use the os.listdir() function which is in the os library. This function returns a list of names of files present in the directory and no order.
So to get the specific type of file from a particular directory we need to iterate through the directory and subdirectory and print the file with a specific extension.
Syntax:
listdir(path)
Approach
Import the os library and pass the directory in the os.listdir() function.
Create a tuple having the extensions that you want to fetch.
Through a loop iterate over all the files in the directory and print the file having a particular extension.
The endswith() function checks if the file ends that particular extension or not is its does then it prints the file name.
Example:
Python3
# importing the libraryimport os # giving directory namedirname = 'D:\\AllData' # giving file extensionext = ('.exe', 'jpg') # iterating over all filesfor files in os.listdir(dirname):    if files.endswith(ext):        print(files)  # printing file name of desired extension    else:        continue
Output:
Method 2: Using scandir()
This method uses os.scandir() function returns an iterator that is used to access the file. The entries are yielded in arbitrary order. It lists the directories or files immediately under that directory.
Syntax:
scandir(path)
Example:
Python3
# importing the moduleimport os # directory namedirname = 'D:\\AllData' # extensionsext = ('.exe', 'jpg') # scanning the directory to get required filesfor files in os.scandir(dirname):    if files.path.endswith(ext):        print(files)  # printing file name
Output:
Method 3: Using walk()
In this method we will use the os.walk() function which yields us three tuples namely:-(dirpath, dirnames, filenames). Since it is a recursive process it will iterate over all descendant files in subdirectories and print the file name. A further approach is the same as the above method.
Syntax:
walk(path)
Example:
Python3
# importing the moduleimport os # giving directory namefolderdir = 'D:\\AllData' # giving file extensionsext = ('.pdf', '.mkv') # iterating over directory and subdirectory to get desired resultfor path, dirc, files in os.walk(folderdir):    for name in files:        if name.endswith(ext):            print(name)  # printing file name
Output:
Method 4: Using glob 
In this method, we will use the glob.iglob() function which comes under the glob library. Glob is a general term used to define techniques to match specified patterns according to rules related to Unix shell. Linux and Unix systems and shells also support glob and also provide function glob() in system libraries.
In Python, the glob module is used to retrieve files/pathnames matching a specified pattern. The glob function accepts the directory/path and the \\**\\ pattern tells to look for the files with a specific extension in subfolders also that needs to be a recursive process so recursive should be set to True.
Example:
Python3
# importing the moduleimport glob # accessing and printing files in directory and subdirectoryfor filename in glob.glob('D:\\AllData\\**\\*.exe', recursive=True):    print(filename)  # print file name
Output:
Method 5: Using path()
This method uses the Path() function from the pathlib module. The path function accepts the directory name as an argument and in glob function ‘**/*’ pattern is used to find files of specific extensions. It is also a recursive function and lists all files of the same directory and sub-directory.
Syntax:
path(path)
Example:
Python3
# importing the modulefrom pathlib import Path # directory namedirname = 'D:\\AllData' # giving directory name to Path() functionpaths = Path(dirname).glob('**/*.exe',) # iterating over all filesfor path in paths:    print(path)  # printing file name
Output:
punamsingh628700
mitalibhola94
Picked
Python file-handling-programs
python-file-handling
Technical Scripter 2020
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.