How to use Glob() function to find files recursively in Python?
Last Updated :
02 Aug, 2024
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 pattern rules of glob follow standard Unix path expansion rules. It is also predicted that according to benchmarks it is faster than other methods to match pathnames in directories. With glob, we can also use wildcards
("*, ?, [ranges])
apart from exact string search to make path retrieval more simple and convenient.
Note:
This module comes built-in with Python, so there is no need to install it externally.
Example:
Python3 1==
# Python program to demonstrate
# glob using different wildcards
import glob
print('Named explicitly:')
for name in glob.glob('/home/geeks/Desktop/gfg/data.txt'):
print(name)
# Using '*' pattern
print('\nNamed with wildcard *:')
for name in glob.glob('/home/geeks/Desktop/gfg/*'):
print(name)
# Using '?' pattern
print('\nNamed with wildcard ?:')
for name in glob.glob('/home/geeks/Desktop/gfg/data?.txt'):
print(name)
# Using [0-9] pattern
print('\nNamed with wildcard ranges:')
for name in glob.glob('/home/geeks/Desktop/gfg/*[0-9].*'):
print(name)
Output :

Using Glob() function to find files recursively
We can use the function
glob.glob()
or
glob.iglob()
directly from glob module to retrieve paths recursively from inside the directories/files and subdirectories/subfiles.
Syntax:
glob.glob(pathname, *, recursive=False)
glob.iglob(pathname, *, recursive=False)
Note:
When recursive is set
True
"
**
" followed by path separator
('./**/')
will match any files or directories.
Example:
Python3 1==
# Python program to find files
# recursively using Python
import glob
# Returns a list of names in list files.
print("Using glob.glob()")
files = glob.glob('/home/geeks/Desktop/gfg/**/*.txt',
recursive = True)
for file in files:
print(file)
# It returns an iterator which will
# be printed simultaneously.
print("\nUsing glob.iglob()")
for filename in glob.iglob('/home/geeks/Desktop/gfg/**/*.txt',
recursive = True):
print(filename)
Output :

For older versions of python:
The most simple method is to use
os.walk() as it is specifically designed and optimized to allow recursive browsing of a directory tree. Or we can also use
os.listdir() to get all the files in directory and subdirectories and then filter out. Let us see it through an example-
Example:
Python3 1==
# Python program to find files
# recursively using Python
import os
# Using os.walk()
for dirpath, dirs, files in os.walk('src'):
for filename in files:
fname = os.path.join(dirpath,filename)
if fname.endswith('.c'):
print(fname)
"""
Or
We can also use fnmatch.filter()
to filter out results.
"""
for dirpath, dirs, files in os.walk('src'):
for filename in fnmatch.filter(files, '*.c'):
print(os.path.join(dirpath, filename))
# Using os.listdir()
path = "src"
dir_list = os.listdir(path)
for filename in fnmatch.filter(dir_list,'*.c'):
print(os.path.join(dirpath, filename))
Output :
./src/add.c
./src/subtract.c
./src/sub/mul.c
./src/sub/div.c
./src/add.c
./src/subtract.c
./src/sub/mul.c
./src/sub/div.c
./src/add.c
./src/subtract.c
./src/sub/mul.c
./src/sub/div.c
Similar Reads
Find path to the given file using Python We can get the location (path) of the running script file .py with __file__. __file__ is useful for reading other files and it gives the current location of the running file. It differs in versions. In Python 3.8 and earlier, __file__ returns the path specified when executing the Python command. We
5 min read
How to read multiple text files from folder in Python? Reading multiple text files from a folder in Python means accessing all the .txt files stored within a specific directory and processing their contents one by one. For example, if a folder contains three text files, each with a single line of text, you might want to read each fileâs content and proc
3 min read
How to Get directory of Current Script in Python? A Parent directory is a directory above another file/directory in a hierarchical file system. Getting the Parent directory is essential in performing certain tasks related to filesystem management. In this article, we will take a look at methods used for obtaining the Parent directory of the curren
4 min read
How to List all Files and Directories in FTP Server using Python? FTP ( File Transfer Protocol ) is set of rules that computer follows to transfer files across computer network. It is TCP/IP based protocol. FTP lets clients share files. FTP is less secure because of files are shared as plain text without any encryption across the network. It is possible using pyt
2 min read
How to get size of folder using Python? In this article, we are going to discuss various approaches to get the size of a folder using python. To get the size of a directory, the user has to walk through the whole folder and add the size of each file present in that folder and will show the total size of the folder. Steps to be followed:
3 min read
How to Delete files in Python using send2trash module? In this article, we will see how to safely delete files and folders using the send2trash module in Python. Using send2trash, we can send files to the Trash or Recycle Bin instead of permanently deleting them. The OS module's unlink(), remove() and rmdir() functions can be used to delete files or fol
2 min read
How to iterate over files in directory using Python? Iterating over files in a directory using Python involves accessing and processing files within a specified folder. Python provides multiple methods to achieve this, depending on efficiency and ease of use. These methods allow listing files, filtering specific types and handling subdirectories.Using
3 min read
How to move all files from one directory to another using Python ? In this article, we will see how to move all files from one directory to another directory using Python. Â In our day-to-day computer usage we generally copy or move files from one folder to other, now let's see how to move a file in Python: This can be done in two ways:Using os module.Using shutil m
2 min read
Python List All Files In Directory And Subdirectories Listing all files in a directory and its subdirectories is a common task in Python, often encountered in file management, data processing, or system administration. As developers, having multiple approaches at our disposal allows us to choose the most suitable method based on our specific requiremen
5 min read
Python - Get list of files in directory sorted by size In this article, we will be looking at the different approaches to get the list of the files in the given directory in the sorted order of size in the Python programming language. The two different approaches to get the list of files in a directory are sorted by size is as follows: Using os.listdir(
3 min read