Python

Python Glob Recursive

Glob is a word that describes ways to match patterns in a Unix shell. Linux and Unix use glob systems and have libraries that support/provide the glob() function. The glob() function is a part of the “glob” module in Python that is used to find files and paths that match a specific pattern.

This guide will present a precise tutorial on Python glob.glob() method using numerous examples and following the below contents:

What is the glob.glob() Method in Python?

In Python, the “glob.glob()” method is utilized to determine all the path names matching a particular pattern based on the rules used by the Unix shell. The particular pattern can be a simple filename, or it can use wildcard characters such as *, ?, and [].

Syntax

glob.glob(pathname, *, recursive=False)

Parameters

In this syntax, the “pathname” parameter indicates the absolute path with a complete path along with the filename. The “recursive=False” parameter indicates whether the file is searched recursively or not.

Return Value

The “glob.glob()” method retrieves the files list or folder list that matches the specified path.

Example 1: Listing All the Files of the Specified Path Using the glob.glob() Method

In the below code, the “glob” module is imported at the start. After that, the “glob.glob()” function is used to determine all the files in the particular directory that match/trace the pattern *. Here, the asterisk (*) used can match any character as a wildcard.

import glob
for i in glob.glob(r'C:\Users\p\Documents\program\*'):
    print(i)

The above code execution retrieves all the files present in the specified directory:

Example 2: Listing All the Particular Files of the Specified Path Using the glob.glob() Method

In the below code, the “*.csv” pattern is passed along with the absolute path of the directory to match all the “CSV” files and retrieve the output:

import glob
for i in glob.glob(r'C:\Users\p\Documents\program\*.csv'):
    print(i)

The above code execution retrieved all the files with the “CSV” extension format:

Example 3: Search Files Recursively Using the glob.glob() Method

In the below code, the “glob” module is assigned, and the variable path is assigned a string value. The “glob.glob()” function is used to search for files matching the particular pattern in the given/specified path. Here, the “recursive=True” parameter ensures the search is performed recursively, including subdirectories:

import glob
path = r'C:\Users\p\Documents\program\**\*.xlsx'
for f in glob.glob(path, recursive=True):
    print(f)

All the particular files placed in the directories and sub-directories have been retrieved successfully:

Example 4: Search All Files Start With Specific Words Using the glob.glob() Method

In this code, the new* pattern is used to match files starting with “new” and followed by any characters. Next, the glob.glob() function is called to retrieve a list of filenames that match the specified pattern in the given directory:

import glob
path = r'C:\Users\p\Documents\program\new*'
for f in glob.glob(path):
    print(f)

The below output displayed the files starting with the “new”:

Example 5: Search All Files Containing Three Character Words Using the glob.glob() Method

In this code, the variable “path” represents the directory path with a wildcard pattern “???”. This pattern is used to match any three characters in the file name:

import glob
path = r'C:\Users\p\Documents\program\???.*'
for f in glob.glob(path):
    print(f)

The list of all file path names that match the specified pattern has been displayed:

Example 6: Search All Files With Range of Characters Using the glob.glob() Method

We can also search the files based on the range of characters using the specified pattern “[f-z]”. Here in the below code, the “glob.glob()” method takes the path as an argument and retrieves the list of files that matches the pattern:

import glob
path = r'C:\Users\p\Documents\program\[f-z]*.csv'
for f in glob.glob(path):
    print(f)

File path names matching the pattern have been displayed.

Example 7: Difference Between glob() and iglob() Methods

The main difference between the two methods is that “glob()” returns a list of all the matching files, while “iglob()” returns an iterator that yields the matching files one by one. Here is an example code that demonstrates this difference:

import glob
path = r'C:\Users\p\Documents\program\**\*.xlsx'
output = glob.iglob(path, recursive=True)
print(output, '\n')

output1 = glob.glob(path, recursive=True)
print(output1)

The above code retrieves the below output:

Conclusion

The “glob.glob()” method in Python is utilized to determine all the path names by taking the specified pattern and complete path as an argument. We can use this method to retrieve the list of all files, specified files, or the files based on the wildcard pattern. This write-up delivered a detailed guide on Python glob recursive using numerous examples.

About the author

Haroon Javed

Hi, I'm Haroon. I am an electronics engineer and a technical content writer. I am a tech geek who loves to help people to the best of my knowledge.