← All notes

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

See also

Claims

Sources

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

Permanent ID: iil:note:rgejc38z

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.},
}

Markdown

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.