Python Reference ยท Files, Paths and Text I/O
The glob and fnmatch modules
Reference for Python's glob and fnmatch modules: wildcards, recursive ** patterns, glob.glob, iglob, escape, fnmatch.filter and case sensitivity.
glob finds files whose names match a shell-style pattern such as *.csv or reports/**/summary-?.txt. fnmatch is the matching engine underneath: it tests plain strings against the same kind of pattern without touching the disk.
Use glob to collect files to process, and fnmatch to filter any list of names, such as the results of os.listdir() or an archive listing. These patterns are not regular expressions.
import glob for name in sorted(glob.glob("*.csv")): print(name)
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Pattern syntax
| Pattern | Matches |
|---|---|
* |
Any run of characters (including none), except a path separator in glob. |
? |
Exactly one character. |
[abc], [a-z] |
One character from the set or range. |
[!abc] |
One character not in the set. |
** |
In glob with recursive=True: zero or more folders. |
Syntax
glob.glob(pathname, *, root_dir=None, dir_fd=None, recursive=False, include_hidden=False) glob.iglob(pathname, ...) glob.escape(pathname) fnmatch.fnmatch(name, pattern) fnmatch.fnmatchcase(name, pattern) fnmatch.filter(names, pattern) fnmatch.translate(pattern)
| Name | Description |
|---|---|
pathname |
A pattern that may contain folders, such as "logs/*.log". |
recursive |
If true, ** matches any number of nested folders. |
root_dir |
Folder to search in (Python 3.10+); results are relative to it. |
include_hidden |
If true, * and ** also match names starting with a dot (Python 3.11+). |
names |
An iterable of strings for filter. |
Return value: glob() returns a list of strings; iglob() an iterator; fnmatch() and fnmatchcase() return bool; filter() returns a list; escape() returns the pattern with special characters neutralized.
Usage notes
- No guaranteed order.
globreturns matches in file-system order. Wrap insorted()when output must be stable. - No match is not an error. The result is an empty list.
- Hidden files are skipped unless the pattern itself starts with a dot, or you pass
include_hidden=True(3.11+). **needsrecursive=True. Without it**behaves like a single*. Recursive searches over big trees can be slow;igloblets you stop early.fnmatch()normalizes case on Windows, so"A.TXT"matches"*.txt"there but not on Linux. Usefnmatchcase()for the same behavior everywhere.fnmatchignores slashes:*matches/too, sofnmatch("a/b.txt", "*.txt")is true.globsplits the pattern by folder.- Special characters in real names (
[,*,?) must be escaped withglob.escape()before you build a pattern from a file name. - Alternatives.
pathlib.Path.glob()andrglob()offer the same patterns and returnPathobjects. Brace patterns like*.{png,jpg}are not supported; run two patterns or usefnmatchwith a list.