Python Glossary Русский

Python 3.13+globfunction

glob.translate()

file search

Translates a glob pattern into a regular expression string for matching paths without touching the disk (3.13+).

Syntax

glob.translate(pathname, *, recursive=False, include_hidden=False, seps=None) -> str

Unlike fnmatch.translate, it respects path separators: * does not cross '/', and ** works only with recursive=True. Hidden files do not match by default, as with glob(); include_hidden=True changes that. It returns a string that you compile with re.compile().

Examples

import glob, re
rx = re.compile(glob.translate('**/*.py', recursive=True, include_hidden=False))
print(bool(rx.match('pkg/mod.py')), bool(rx.match('pkg/.hidden.py')))  # → True False
flat = re.compile(glob.translate('*.py'))
print(bool(flat.match('pkg/mod.py')))  # → False — * не проходит через /

See also

Open in the full glossarydocs.python.orgReport an inaccuracy