Python Glossary Русский

all Python 3fnmatchfunction

fnmatch.fnmatch()

filename patterns

Tests whether a name matches a shell-style pattern: *, ?, [seq]. Case sensitivity follows the current OS.

Syntax

fnmatch.fnmatch(name, pat)

Case sensitivity depends on the system: on Windows 'A.PY' matches '*.py', on Linux and macOS it does not; fnmatchcase() behaves the same everywhere. Unlike glob, the star also crosses '/', and dot-files are not hidden: '*.py' matches 'src/a.py' and '.hidden.py'. It works on strings and never reads the disk.

Examples

import fnmatch
print(fnmatch.fnmatch('report.py', '*.py'))  # → True
print(fnmatch.fnmatch('src/a.py', '*.py'))  # → True — * проходит через /
print(fnmatch.fnmatch('A.PY', '*.py'))  # → ? False на Linux и macOS, True на Windows

See also

Open in the full glossarydocs.python.orgReport an inaccuracy