Skip to content
Tutorials Center
My learning

Topics

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.

Python
import glob

for name in sorted(glob.glob("*.csv")):
    print(name)

Try it

Python Searching folders with glob

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.

Python Filtering names with fnmatch

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

Python
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. glob returns matches in file-system order. Wrap in sorted() 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+).
  • ** needs recursive=True. Without it ** behaves like a single *. Recursive searches over big trees can be slow; iglob lets you stop early.
  • fnmatch() normalizes case on Windows, so "A.TXT" matches "*.txt" there but not on Linux. Use fnmatchcase() for the same behavior everywhere.
  • fnmatch ignores slashes: * matches / too, so fnmatch("a/b.txt", "*.txt") is true. glob splits the pattern by folder.
  • Special characters in real names ([, *, ?) must be escaped with glob.escape() before you build a pattern from a file name.
  • Alternatives. pathlib.Path.glob() and rglob() offer the same patterns and return Path objects. Brace patterns like *.{png,jpg} are not supported; run two patterns or use fnmatch with a list.