@@ -34,9 +34,7 @@ unlike :func:`fnmatch.fnmatch` or :func:`pathlib.Path.glob`.
3434For a literal match, wrap the meta-characters in brackets.
3535For example, ``'[?]' `` matches the character ``'?' ``.
3636
37-
38- .. seealso ::
39- The :mod: `pathlib ` module offers high-level path objects.
37+ The :mod: `glob ` module defines the following functions:
4038
4139
4240.. function :: glob(pathname, *, root_dir=None, dir_fd=None, recursive=False, \
@@ -117,7 +115,48 @@ For example, ``'[?]'`` matches the character ``'?'``.
117115 .. versionadded :: 3.4
118116
119117
120- For example, consider a directory containing the following files:
118+ .. function :: translate(pathname, *, recursive=False, include_hidden=False, seps=None)
119+
120+ Convert the given path specification to a regular expression for use with
121+ :func: `re.match `. The path specification can contain shell-style wildcards.
122+
123+ For example:
124+
125+ >>> import glob, re
126+ >>>
127+ >>> regex = glob.translate(' **/*.txt' , recursive = True , include_hidden = True )
128+ >>> regex
129+ '(?s:(?:.+/)?[^/]*\\.txt)\\Z'
130+ >>> reobj = re.compile(regex)
131+ >>> reobj.match(' foo/bar/baz.txt' )
132+ <re.Match object; span=(0, 15), match='foo/bar/baz.txt'>
133+
134+ Path separators and segments are meaningful to this function, unlike
135+ :func: `fnmatch.translate `. By default wildcards do not match path
136+ separators, and ``* `` pattern segments match precisely one path segment.
137+
138+ If *recursive * is true, the pattern segment "``** ``" will match any number
139+ of path segments. If "``** ``" occurs in any position other than a full
140+ pattern segment, :exc: `ValueError ` is raised.
141+
142+ If *include_hidden * is true, wildcards can match path segments that start
143+ with a dot (``. ``).
144+
145+ A sequence of path separators may be supplied to the *seps * argument. If
146+ not given, :data: `os.sep ` and :data: `~os.altsep ` (if available) are used.
147+
148+ .. seealso ::
149+
150+ :meth: `pathlib.PurePath.match ` and :meth: `pathlib.Path.glob ` methods,
151+ which call this function to implement pattern matching and globbing.
152+
153+ .. versionadded :: 3.13
154+
155+
156+ Examples
157+ --------
158+
159+ Consider a directory containing the following files:
121160:file: `1.gif `, :file: `2.txt `, :file: `card.gif ` and a subdirectory :file: `sub `
122161which contains only the file :file: `3.txt `. :func: `glob ` will produce
123162the following results. Notice how any leading components of the path are
@@ -146,6 +185,7 @@ default. For example, consider a directory containing :file:`card.gif` and
146185 ['.card.gif']
147186
148187.. seealso ::
188+ The :mod: `fnmatch ` module offers shell-style filename (not path) expansion.
149189
150- Module :mod: ` fnmatch `
151- Shell-style filename (not path) expansion
190+ .. seealso ::
191+ The :mod: ` pathlib ` module offers high-level path objects.
0 commit comments