How to find all files with extensions in Python

To find all the files with extensions in Python, use the os.listdir() method. The os.listdir() is a built-in Python function that specifies all the files in a directory and returns a list containing the names of the files in the directory given by path.

There is also another module called glob which provides a glob.glob() function that can be used to find all the files with extension. The glob module identifies pathnames whose patterns match a given pattern, as defined by the Unix shell.

You can also use the os.listdir() method to list all the files in Python.

A Directory is an entity that contains a collection of individual files and folders hierarchically and logically. There are many logical structures such as Single-level directory, Two-level directory, Tree-structured directory, Acyclic graph directory, and many more.

 In this blog, we will work with the SINGLE-LEVEL Directory structure. The single-level directory is the simplest directory structure. Moreover, all files are contained in the same directory, making it easier to maintain and understand.

For example, see the below screenshot.

How to find all files with extension in Python

In the above example, a directory called the “Learning” directory contains various no. of folders such as “C#”, “Cloud Computing”, “Competitive Programming”, “Data Structure”, “Development”, “Electronics Training” and many more. Below each of these folders is numerous files with varying extension names, such as ‘.txt’, ‘.bin’, ‘.py’, and many more.

Using os.listdir() method

The os.listdir() method returns a list containing the names of the entries in the directory given by the path. In addition, it returns all the files with extensions in Python. The OS is a built-in Python module that provides a portable way of implementing operating system-dependent features.

import os

path = "/Users/krunallathiya/Desktop/Code/R"

print(os.listdir(path))

Output

['data.R', 'data.json', 'app.js', 'data.py', 'Pro.R']

We get the list of all the files with extensions using the os.listdir() method.

Using glob.glob() function

The glob.glob() is a built-in Python function that retrieves paths recursively from inside the directories/files and subdirectories/subfiles.

import glob

path = "/Users/krunallathiya/Desktop/Code/R"

print(glob.glob(r"*"))

Output

['data.R', 'data.json', 'app.js', 'data.py', 'Pro.R']

It returns all the files contained within a directory.

To find a file with a specific file extension in Python, use the glob.glob() function and pass the file extension after ‘*’ in the argument.

import glob

path = "/Users/krunallathiya/Desktop/Code/R"

print(glob.glob("*.R"))

Output

['data.R', 'Pro.R']

The code returns only the file names with .txt as file extension in the above example, i.e., “data.R“, “Pro.R“.

Conclusion

In this article, we learned about how to find all the files with extensions in Python using”

  • os.listdir() method
  • glob.glob() function

NOTE: There is no need the exclusively install these modules because these are built-in modules and when you install Python, these are automatically installed.

That’s it for this tutorial.

See also

How to make requirements.txt file

How to close all subprocesses in Python

Leave a Comment