Python fnmatch - glob-style name matching with *, ? and brackets
Answer: This module provides support for Unix shell-style wildcards. fnmatch(filename, pattern) matches according to the local convention, and translate(PATTERN) returns a regular expression corresponding to PATTERN.
Examples
import fnmatch, os
fnmatch.fnmatch("README.md", "*.md") # True
fnmatch.fnmatchcase("README.md", "*.MD") # False on a case-sensitive match
fnmatch.filter(os.listdir("."), "test_*.py")
print(fnmatch.translate("*.pyc")) # regular expression
Rules
*matches everything,?matches a single character, and brackets match a character class.fnmatchcaseis a case-sensitive variant.filter(names, pattern)returns the matching names.
See also
Claims
- id: C1 claim: fnmatch supports Unix shell-style wildcards. source: S1 evidence: "This module provides support for Unix shell-style wildcards"
- id: C2 claim: translate converts a pattern to a regular expression. source: S2 evidence: "translate(PATTERN) returns a regular expression corresponding to PATTERN"
- id: C3 claim: fnmatch matches a filename to a pattern. source: S2 evidence: "fnmatch(FILENAME, PATTERN) matches according to the local convention"
Sources
- [S1] fnmatch - Unix filename pattern matching. Python docs. https://docs.python.org/3/library/fnmatch.html . License: PSF-2.0. Accessed 2026-11-18.
- [S2] Lib/fnmatch.py. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Lib/fnmatch.py . License: PSF-2.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, fnmatch - Unix filename pattern matching; python/cpython (GitHub), Lib/fnmatch.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python docs, fnmatch - Unix filename pattern matching; python/cpython (GitHub), Lib/fnmatch.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python fnmatch - glob-style name matching with *, ? and brackets." iinventedlight Library, iil:note:rgejc38z, version of 2026-11-18. https://iinventedlight.com/library/notes/python-fnmatch/. License: CC-BY-4.0.
@misc{iil_note_rgejc38z,
author = {{iinventedlight}},
title = {{Python fnmatch - glob-style name matching with *, ? and brackets}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/python-fnmatch/},
note = {Permanent ID iil:note:rgejc38z. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-fnmatch.md.