Open In App

Python – Loop through files of certain extensions

Last Updated : 10 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:



Previous Article
Next Article

Similar Reads

Loop Through a List using While Loop in Python
In Python, the while loop is a versatile construct that allows you to repeatedly execute a block of code as long as a specified condition is true. When it comes to looping through a list, the while loop can be a handy alternative to the more commonly used for loop. In this article, we'll explore four simple examples of how to loop through a list us
3 min read
Python Loop through Folders and Files in Directory
File iteration is a crucial process of working with files in Python. The process of accessing and processing each item in any collection is called File iteration in Python, which involves looping through a folder and perform operation on each file. In this article, we will see how we can iterate over files in a directory in Python. Python Loop thro
4 min read
Loop or Iterate over all or certain columns of a dataframe in Python-Pandas
Pandas DataFrames facilitate column-wise iteration, allowing convenient access to elements in each column. In this article, we will discuss how to loop or Iterate overall or certain columns of a DataFrame. Creating Pandas DataframeIn this article, we will use this Dataframe that we have created by using the Pandas package. C/C++ Code # import panda
5 min read
List all files of certain type in a directory using Python
In python, there are several built-in modules and methods for file handling. These functions are present in different modules such as os, glob, etc. This article helps you find out many of the functions in one place which gives you a brief knowledge about how to list all the files of a certain type in a directory by using Python programming. So the
3 min read
Python | Arrange the files in directories according to extensions
In this article, we will learn how to arrange the files in different directories according to their extensions. When you have a lot of files with different extension than you can simply use the script for your help. Below is the Python implementation - import os # Using system functions import shutil # Moving file to different folders. # Changing t
2 min read
Loop through a JSON array in Python
A JSON array is an ordered list of values that can store multiple values such as string, number, boolean, or object. The values in a JSON array must be separated by commas and enclosed in squares in brackets []. In this article, we will learn how we can loop through a JSON array in Python. Iterate over a JSON object in PythonArrays in JSON are almo
4 min read
Loop Through a Nested Dictionary in Python
Working with nested dictionaries in Python can be a common scenario, especially when dealing with complex data structures. Iterating through a nested dictionary efficiently is crucial for extracting and manipulating the desired information. In this article, we will explore five simple and generally used methods to loop through a nested dictionary i
3 min read
Difference between for loop and while loop in Python
In this article, we will learn about the difference between for loop and a while loop in Python. In Python, there are two types of loops available which are 'for loop' and 'while loop'. The loop is a set of statements that are used to execute a set of statements more than one time. For example, if we want to print "Hello world" 100 times then we ha
4 min read
How to merge multiple excel files into a single files with Python ?
Normally, we're working with Excel files, and we surely have come across a scenario where we need to merge multiple Excel files into one. The traditional method has always been using a VBA code inside excel which does the job but is a multi-step process and is not so easy to understand. Another method is manually copying long Excel files into one w
4 min read
How to loop through each row of dataFrame in PySpark ?
In this article, we are going to see how to loop through each row of Dataframe in PySpark. Looping through each row helps us to perform complex operations on the RDD or Dataframe. Creating Dataframe for demonstration: C/C++ Code # importing necessary libraries import pyspark from pyspark.sql import SparkSession # function to create new SparkSession
5 min read