Skip to content

Commit 2699643

Browse files
[3.14] docs: be clearer that glob results are unordered (pythonGH-140184) (python#140339)
(cherry picked from commit ed672f7) Co-authored-by: Ned Batchelder <[email protected]>
1 parent 13e1642 commit 2699643

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

Doc/library/glob.rst

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,27 @@
1818
single: - (minus); in glob-style wildcards
1919
single: . (dot); in glob-style wildcards
2020

21-
The :mod:`glob` module finds all the pathnames matching a specified pattern
22-
according to the rules used by the Unix shell, although results are returned in
23-
arbitrary order. No tilde expansion is done, but ``*``, ``?``, and character
21+
The :mod:`!glob` module finds pathnames
22+
using pattern matching rules similar to the Unix shell.
23+
No tilde expansion is done, but ``*``, ``?``, and character
2424
ranges expressed with ``[]`` will be correctly matched. This is done by using
2525
the :func:`os.scandir` and :func:`fnmatch.fnmatch` functions in concert, and
2626
not by actually invoking a subshell.
2727

28-
Note that files beginning with a dot (``.``) can only be matched by
28+
.. note::
29+
The pathnames are returned in no particular order. If you need a specific
30+
order, sort the results.
31+
32+
Files beginning with a dot (``.``) can only be matched by
2933
patterns that also start with a dot,
3034
unlike :func:`fnmatch.fnmatch` or :func:`pathlib.Path.glob`.
31-
(For tilde and shell variable expansion, use :func:`os.path.expanduser` and
32-
:func:`os.path.expandvars`.)
35+
For tilde and shell variable expansion, use :func:`os.path.expanduser` and
36+
:func:`os.path.expandvars`.
3337

3438
For a literal match, wrap the meta-characters in brackets.
3539
For example, ``'[?]'`` matches the character ``'?'``.
3640

37-
The :mod:`glob` module defines the following functions:
41+
The :mod:`!glob` module defines the following functions:
3842

3943

4044
.. function:: glob(pathname, *, root_dir=None, dir_fd=None, recursive=False, \
@@ -51,7 +55,7 @@ The :mod:`glob` module defines the following functions:
5155

5256
If *root_dir* is not ``None``, it should be a :term:`path-like object`
5357
specifying the root directory for searching. It has the same effect on
54-
:func:`glob` as changing the current directory before calling it. If
58+
:func:`!glob` as changing the current directory before calling it. If
5559
*pathname* is relative, the result will contain paths relative to
5660
*root_dir*.
5761

Lib/glob.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ def glob(pathname, *, root_dir=None, dir_fd=None, recursive=False,
2222
dot are special cases that are not matched by '*' and '?'
2323
patterns by default.
2424
25+
The order of the returned list is undefined. Sort it if you need a
26+
particular order.
27+
2528
If `include_hidden` is true, the patterns '*', '?', '**' will match hidden
2629
directories.
2730
@@ -40,6 +43,9 @@ def iglob(pathname, *, root_dir=None, dir_fd=None, recursive=False,
4043
dot are special cases that are not matched by '*' and '?'
4144
patterns.
4245
46+
The order of the returned paths is undefined. Sort them if you need a
47+
particular order.
48+
4349
If recursive is true, the pattern '**' will match any files and
4450
zero or more directories and subdirectories.
4551
"""

0 commit comments

Comments
 (0)