Signup/Sign In

How To Find files with Certain Extension using Python

We know how to find files and do operations on that file using the file handling methods. But when we need to do certain operations on a file with a specific file extension like .txt, .PNG, .JPG, or .py, we need to find that file.

As we all know there are several methods to find the files, but in this tutorial, we will learn to find the files with certain extensions using the endswith() function, comprehension method, and the several methods present in the os module and glob module.

The endswith() is an in-built function that returns True in this case, if the string ends with a particular specified suffix; else it will return False. The glob.glob() function returns the file name with a specified pattern.

Example: Finding files in a directory using the listdir() Function

The below example shows how to find files in the directory using the listdir() function.

# Lists all files
#import os module
import os
# Specifies the path in path variable
path="C:\my_dir"
for x in os.listdir(path):
    print(x)

Once we run the program we will get the following output.


instance_var_examples.py
mp4_1.mp4
mp4_5.mp4
practice.py
practice1.py
write opeartion.png

Example: Finding file using the endswith() Function

The below example shows how to find files in the directory with certain extension using the listdir() function and the endswith() function.

# Finding files with extension using for loop
#import os module
import os
# Specifies the path in path variable
path="C:\my_dir"
for i in os.listdir(path):
    # List files with .py
    if i.endswith(".py"):
        print("Files with extension .py are:",i)

Once we run the program we will get the following output.


Files with extension .py are: instance_var_examples.py
Files with extension .py are: practice.py
Files with extension .py are: practice1.py
Files with extension .py are: Static_var.py
Files with extension .py are: variables_2.py
Files with extension .py are: var_1.py

Example: Finding files with a certain extension using the comprehension

The below example shows how to find files in the directory with a certain extension using the listdir() function and the endswith() function with the comprehension method.

# Using comprehension method
import os
path = 'C:\my_dir'
files = [x for x in os.listdir(path) if x.endswith('.py')]
print(files)

Once we run the program we will get the following output.


['instance_var_examples.py', 'practice.py', 'practice1.py', 'Static_var.py', 'variables_2.py', 'var_1.py']

Example: Finding files with a certain extension using the glob and os Module

The below example shows how to find files in the directory with a certain extension using the glob and os module.

# Another method using glob and os module
#import glob and os module
import glob
import os
os.chdir("C:\my_dir")
for file in glob.glob("*.py"):
    print(file)

Once we run the program we will get the following output.


instance_var_examples.py
practice.py
practice1.py
Static_var.py
variables_2.py
var_1.py

Conclusion

In this tutorial, we learned how to find the files with specified extensions using the python in-built function endswith() and the glob and os module.



About the author:
An enthusiastic fresher, a patient person who loves to work in diverse fields. I am a creative person and always present the work with utmost perfection.