Python – Loop through files of certain extensions
Last Updated :
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
import os
dirname = 'D:\\AllData'
ext = ( '.exe' , 'jpg' )
for files in os.listdir(dirname):
if files.endswith(ext):
print (files)
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
import os
dirname = 'D:\\AllData'
ext = ( '.exe' , 'jpg' )
for files in os.scandir(dirname):
if files.path.endswith(ext):
print (files)
|
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
import os
folderdir = 'D:\\AllData'
ext = ( '.pdf' , '.mkv' )
for path, dirc, files in os.walk(folderdir):
for name in files:
if name.endswith(ext):
print (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
import glob
for filename in glob.glob( 'D:\\AllData\\**\\*.exe' , recursive = True ):
print (filename)
|
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
from pathlib import Path
dirname = 'D:\\AllData'
paths = Path(dirname).glob( '**/*.exe' ,)
for path in paths:
print (path)
|
Output:
Please Login to comment...