Python glob: How to Use Pattern Matching for File and Directory Search
ArticleLearn how to use Python’s glob module for file and directory searches using wildcard patterns, recursive matching, and efficient iterators.
If you’ve ever needed to find or filter files in a directory using flexible rules, Python’s built-in glob module is one of the simplest and most effective tools available. It lets you search for filenames and paths that match shell-style wildcard patterns—similar to typing commands in a Unix or Linux shell—without writing complex directory traversal logic by hand.
In this article, you’ll learn what glob does, why it’s useful, how its pattern matching works, and how to use it in real Python scripts. We’ll also look at recursive searching and iterator-based alternatives for more efficient file handling.
What the glob Module Is
The glob module is part of the Python standard library and provides functions to find pathnames matching a specified pattern according to rules similar to those used by the Unix shell.
Patterns can include special wildcard characters such as:
*— matches zero or more characters?— matches exactly one character[abc]or[a-z]— character ranges or sets
Unlike scanning directories manually with os.listdir() and filtering results yourself, glob applies the pattern directly and returns only the file and directory names that match.
Note: Results may be returned in arbitrary order. If ordering matters, wrap the result with
sorted().
Basic Usage of glob.glob()
The most common function is glob.glob(). It takes a pattern string that represents the file search and returns a list of matching paths.
Example: Find Files with a Specific Extension
import glob
# List all Python files in the current directory
python_files = glob.glob("*.py")
print("Python files:", python_files)
Explanation:
*.pymatches any filename ending in.py- The returned list contains all matching files in the current directory
Wildcards and Character Ranges
You can combine wildcards and ranges to build more precise filters.
import glob
# Files starting with a digit and any extension
digit_files = glob.glob("[0-9]*.*")
print("Digit files:", digit_files)
# Files with exactly four characters before .txt
four_char_txt = glob.glob("????.txt")
print("Four char .txt files:", four_char_txt)
Explanation:
[0-9]*.*matches filenames starting with a number????.txtmatches files with exactly four characters before.txt
Recursive Searching with **
Since Python 3.5, glob supports recursive searches using the ** pattern together with recursive=True.
Example: Recursive Search
import glob
# Find all text files in current and subdirectories
text_files = glob.glob("**/*.txt", recursive=True)
for file in text_files:
print(file)
Explanation:
**/*.txtsearches all directories and subdirectoriesrecursive=Trueenables recursive matching
This makes it easy to scan deep directory structures with minimal code.
Using an Iterator with glob.iglob()
If you expect a large number of matches and want to avoid loading them all into memory, use glob.iglob(), which returns an iterator.
import glob
for path in glob.iglob("**/*.log", recursive=True):
print("Log file:", path)
This approach is ideal for large file systems or memory-sensitive workflows.
Advanced Pattern Matching
Beyond * and ?, you can use character sets for more targeted matching.
import glob
# Match files ending in .jpg or .png
image_files = glob.glob("*.[jp][pn]g")
print(image_files)
Here, *.[jp][pn]g matches common image file extensions.
If you need to match wildcard characters literally (for example, a filename that actually contains *), you can use glob.escape() to prevent special interpretation.
Using glob with Other Python Modules
glob is often combined with modules like os, shutil, or pathlib for file operations.
In modern Python, pathlib offers:
Path.glob()Path.rglob()
These methods integrate glob-style pattern matching into object-oriented path handling, improving readability and maintainability.
Common Use Cases
The glob module is commonly used for:
- Listing files with a specific extension
- Searching nested directories
- Filtering files before batch processing
- Collecting paths for automation, testing, or data ingestion
Because it’s included in the standard library, glob requires no additional dependencies and is ideal for quick scripting tasks.
Conclusion
Python’s glob module provides a powerful yet approachable way to search for files and directories using familiar wildcard patterns. Whether you need a quick list of scripts, a recursive log search, or filtered file collections for automation, glob delivers flexibility with minimal code.
For more advanced path handling and modern workflows, combining glob with pathlib offers both expressive pattern matching and clean, readable filesystem logic.
Find more insights here
What Is Parsing? A Clear Definition and Practical Understanding
Learn what parsing means in computer science and language, how it works, common parser types, and re...
What Is a Rank Tracker API and How It Powers Modern SEO
Learn what a Rank Tracker API is, how it works, key features to look for, and how developers use it...
What Is a Rank Checker API and Why It Matters for SEO
Learn what a Rank Checker API is, how it works, and why SEO teams use it to automate keyword trackin...