Python Glossary Русский

all Python 3fnmatchterm

fnmatch

module

Matching names against shell-style patterns such as *.py and data_??.csv: checking one name and filtering a list.

Syntax

import fnmatch

fnmatch compares strings with a pattern and never touches the disk; to find files in a directory by the same pattern use glob or Path.glob. The pattern is simpler than a regular expression: *, ?, [abc]. The trap is case: fnmatch() follows the OS rules and ignores case on Windows, while fnmatchcase() always respects it.

Examples

import fnmatch
names = ['main.py', 'test_main.py', 'README.md']
print(fnmatch.filter(names, '*.py'))  # → ['main.py', 'test_main.py']
print(fnmatch.fnmatchcase('README.md', '*.MD'))  # → False

See also

Open in the full glossarydocs.python.orgReport an inaccuracy