PyDocs

Nav

File and Directory Access

translate

glob

function · Added in 3.13 (source)
  • translate(pathname, *, recursive=False, include_hidden=False, seps=None)
Translate a pathname with shell wildcards to a regular expression.

If `recursive` is true, the pattern segment '**' will match any number
of path segments.

If `include_hidden` is true, wildcards can match path segments beginning
with a dot ('.').

If a sequence of separator characters is given to `seps`, they will be
used to split the pattern into segments and match path separators.  If
not given, os.path.sep and os.path.altsep (where available) are used.
1 Example
>>> import glob, re
>>>
>>> regex = glob.translate('**/*.txt', recursive=True, include_hidden=True)
>>> regex
'(?s:(?:.+/)?[^/]*\\.txt)\\z'
>>> reobj = re.compile(regex)
>>> reobj.prefixmatch('foo/bar/baz.txt')
<re.Match object; span=(0, 15), match='foo/bar/baz.txt'>
from the Python docs
See Also
No see-alsos for glob/translate
0 Notes
No notes for translate