Saturday, December 28, 2024
Google search engine
HomeLanguagesPython – Loop through files of certain extensions

Python – Loop through files of certain extensions

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 library
import os
 
# giving directory name
dirname = 'D:\\AllData'
 
# giving file extension
ext = ('.exe', 'jpg')
 
# iterating over all files
for 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 module
import os
 
# directory name
dirname = 'D:\\AllData'
 
# extensions
ext = ('.exe', 'jpg')
 
# scanning the directory to get required files
for 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 module
import os
 
# giving directory name
folderdir = 'D:\\AllData'
 
# giving file extensions
ext = ('.pdf', '.mkv')
 
# iterating over directory and subdirectory to get desired result
for 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 module
import glob
 
# accessing and printing files in directory and subdirectory
for 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 module
from pathlib import Path
 
# directory name
dirname = 'D:\\AllData'
 
# giving directory name to Path() function
paths = Path(dirname).glob('**/*.exe',)
 
# iterating over all files
for path in paths:
    print(path)  # printing file name


Output:

RELATED ARTICLES

Most Popular

Recent Comments