On this page
fnmatch
Prev: fileinput | Up: stdlib_module | Next: fractions
Rel: pathlib, os, io, time, fileinput
Ref: docs.python.org — fnmatch
Tags: #public #python3 #stdlib
Import
import fnmatch
Overview
Unix shell-style wildcard matching (*, ?, [seq], [!seq]) against plain filename strings (fnmatch, fnmatchcase, filter, translate). Reach for fnmatch when filtering in-memory lists of names (e.g., ignore rules or archive member lists); use pathlib (Path.glob) or glob to match files directly on disk.
Quick Start
import fnmatch
files = ["app.py", "test_app.py", "README.md", "notes.txt"]
assert fnmatch.filter(files, "*.py") == ["app.py", "test_app.py"]
assert fnmatch.fnmatchcase("README.md", "*.md")
assert not fnmatch.fnmatchcase("README.MD", "*.md")
Implementation Walkthrough
1. Filtering Paths Against Multiple Ignore Patterns
import fnmatch
def is_ignored(name: str, patterns: tuple[str, ...]) -> bool:
return any(fnmatch.fnmatchcase(name, pat) for pat in patterns)
ignore = ("*.pyc", "*.tmp", ".DS_Store")
kept = [n for n in ("main.py", "main.pyc", ".DS_Store", "data.json") if not is_ignored(n, ignore)]
assert kept == ["main.py", "data.json"]
2. Compiling a Wildcard to a Regex (fnmatch.translate)
import fnmatch, re
pat = re.compile(fnmatch.translate("log-2026-*.txt"))
assert pat.match("log-2026-12.txt") is not None
Gotchas & Boundaries
fnmatch()is case-insensitive on Windows/macOS and case-sensitive on Linux: Usefnmatchcase()for portable, deterministic case-sensitive matching.*matches/too: Unlike shell globs,fnmatch("a/b/c.py", "*.py")isTruebecausefnmatchtreats/as an ordinary character; usePurePath.full_match()(Python 3.13+) for path-aware**globbing.- Brackets
[...]are special: Escape literal brackets or usere.escape()if matching arbitrary filenames.
Common Functions
- fnmatch.filter() —
filter(names, pat)— Construct a list from those elements of the iterable NAMES that match PAT. - fnmatch.filterfalse() —
filterfalse(names, pat)— Construct a list from those elements of the iterable NAMES that do not match PAT. - fnmatch.fnmatch() —
fnmatch(name, pat)— Test whether FILENAME matches PATTERN. - fnmatch.fnmatchcase() —
fnmatchcase(name, pat)— Test whether FILENAME matches PATTERN, including case. - fnmatch.translate() —
translate(pat)— Translate a shell PATTERN to a regular expression.
Linked from
Related notes
- tuts/python3/stdlib_module/fileinput Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/fractions Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/fnmatch/filter Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/fnmatch/filterfalse Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/fnmatch/fnmatch Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/fnmatch/fnmatchcase Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/fnmatch/translate Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/glob Linked here; shared tags: #stdlib