blob: 3fdba6937c1de0ff931374fe6a2a86b621938cdf [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`glob` --- Unix style pathname pattern expansion
2=====================================================
3
4.. module:: glob
5 :synopsis: Unix shell style pathname pattern expansion.
6
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04007**Source code:** :source:`Lib/glob.py`
Georg Brandl116aa622007-08-15 14:28:22 +00008
9.. index:: single: filenames; pathname expansion
10
Raymond Hettinger4f707fd2011-01-10 19:54:11 +000011--------------
12
Serhiy Storchaka913876d2018-10-28 13:41:26 +020013.. index::
14 single: * (asterisk); in glob-style wildcards
15 single: ? (question mark); in glob-style wildcards
16 single: [] (square brackets); in glob-style wildcards
17 single: ! (exclamation); in glob-style wildcards
18 single: - (minus); in glob-style wildcards
19 single: . (dot); in glob-style wildcards
20
Georg Brandl116aa622007-08-15 14:28:22 +000021The :mod:`glob` module finds all the pathnames matching a specified pattern
Martin Panter9f3c0942015-11-16 23:46:22 +000022according to the rules used by the Unix shell, although results are returned in
23arbitrary order. No tilde expansion is done, but ``*``, ``?``, and character
24ranges expressed with ``[]`` will be correctly matched. This is done by using
Serhiy Storchaka28ab6342016-09-06 22:33:41 +030025the :func:`os.scandir` and :func:`fnmatch.fnmatch` functions in concert, and
Martin Panter9f3c0942015-11-16 23:46:22 +000026not by actually invoking a subshell. Note that unlike :func:`fnmatch.fnmatch`,
27:mod:`glob` treats filenames beginning with a dot (``.``) as special cases.
28(For tilde and shell variable expansion, use :func:`os.path.expanduser` and
Petri Lehtinenee4a20b2013-02-23 19:53:03 +010029:func:`os.path.expandvars`.)
Georg Brandl116aa622007-08-15 14:28:22 +000030
Ezio Melottia39a22d2012-11-17 17:38:11 +020031For a literal match, wrap the meta-characters in brackets.
32For example, ``'[?]'`` matches the character ``'?'``.
33
Georg Brandl116aa622007-08-15 14:28:22 +000034
Antoine Pitrou31119e42013-11-22 17:38:12 +010035.. seealso::
36 The :mod:`pathlib` module offers high-level path objects.
37
38
Serhiy Storchaka8a64cea2020-06-18 22:08:27 +030039.. function:: glob(pathname, *, root_dir=None, dir_fd=None, recursive=False)
Georg Brandl116aa622007-08-15 14:28:22 +000040
41 Return a possibly-empty list of path names that match *pathname*, which must be
42 a string containing a path specification. *pathname* can be either absolute
43 (like :file:`/usr/src/Python-1.5/Makefile`) or relative (like
44 :file:`../../Tools/\*/\*.gif`), and can contain shell-style wildcards. Broken
Elena Oat52465e12018-11-04 06:50:55 -080045 symlinks are included in the results (as in the shell). Whether or not the
Serhiy Storchaka306cfb32020-09-04 21:19:30 +030046 results are sorted depends on the file system. If a file that satisfies
47 conditions is removed or added during the call of this function, whether
48 a path name for that file be included is unspecified.
Georg Brandl116aa622007-08-15 14:28:22 +000049
Serhiy Storchaka8a64cea2020-06-18 22:08:27 +030050 If *root_dir* is not ``None``, it should be a :term:`path-like object`
51 specifying the root directory for searching. It has the same effect on
52 :func:`glob` as changing the current directory before calling it. If
53 *pathname* is relative, the result will contain paths relative to
54 *root_dir*.
55
56 This function can support :ref:`paths relative to directory descriptors
57 <dir_fd>` with the *dir_fd* parameter.
58
Serhiy Storchaka913876d2018-10-28 13:41:26 +020059 .. index::
60 single: **; in glob-style wildcards
61
Serhiy Storchakac2edcdd2014-09-11 12:17:37 +030062 If *recursive* is true, the pattern "``**``" will match any files and zero or
Marce24594b2019-09-11 10:17:05 -070063 more directories, subdirectories and symbolic links to directories. If the
64 pattern is followed by an :data:`os.sep` or :data:`os.altsep` then files will not
65 match.
Georg Brandl116aa622007-08-15 14:28:22 +000066
Steve Dower44f91c32019-06-27 10:47:59 -070067 .. audit-event:: glob.glob pathname,recursive glob.glob
Steve Dower60419a72019-06-24 08:42:54 -070068
Serhiy Storchakac2edcdd2014-09-11 12:17:37 +030069 .. note::
70 Using the "``**``" pattern in large directory trees may consume
71 an inordinate amount of time.
72
73 .. versionchanged:: 3.5
74 Support for recursive globs using "``**``".
75
Serhiy Storchaka8a64cea2020-06-18 22:08:27 +030076 .. versionchanged:: 3.10
77 Added the *root_dir* and *dir_fd* parameters.
Serhiy Storchakac2edcdd2014-09-11 12:17:37 +030078
Serhiy Storchaka8a64cea2020-06-18 22:08:27 +030079
80.. function:: iglob(pathname, *, root_dir=None, dir_fd=None, recursive=False)
Georg Brandl116aa622007-08-15 14:28:22 +000081
Georg Brandl9afde1c2007-11-01 20:32:30 +000082 Return an :term:`iterator` which yields the same values as :func:`glob`
83 without actually storing them all simultaneously.
Georg Brandl116aa622007-08-15 14:28:22 +000084
Steve Dower44f91c32019-06-27 10:47:59 -070085 .. audit-event:: glob.glob pathname,recursive glob.iglob
Steve Dower60419a72019-06-24 08:42:54 -070086
Georg Brandl116aa622007-08-15 14:28:22 +000087
Serhiy Storchakafd32fff2013-11-18 13:06:43 +020088.. function:: escape(pathname)
89
90 Escape all special characters (``'?'``, ``'*'`` and ``'['``).
91 This is useful if you want to match an arbitrary literal string that may
92 have special characters in it. Special characters in drive/UNC
93 sharepoints are not escaped, e.g. on Windows
94 ``escape('//?/c:/Quo vadis?.txt')`` returns ``'//?/c:/Quo vadis[?].txt'``.
95
96 .. versionadded:: 3.4
97
98
Serhiy Storchakac2edcdd2014-09-11 12:17:37 +030099For example, consider a directory containing the following files:
100:file:`1.gif`, :file:`2.txt`, :file:`card.gif` and a subdirectory :file:`sub`
101which contains only the file :file:`3.txt`. :func:`glob` will produce
Georg Brandl116aa622007-08-15 14:28:22 +0000102the following results. Notice how any leading components of the path are
103preserved. ::
104
105 >>> import glob
106 >>> glob.glob('./[0-9].*')
107 ['./1.gif', './2.txt']
108 >>> glob.glob('*.gif')
109 ['1.gif', 'card.gif']
110 >>> glob.glob('?.gif')
111 ['1.gif']
Serhiy Storchakac2edcdd2014-09-11 12:17:37 +0300112 >>> glob.glob('**/*.txt', recursive=True)
113 ['2.txt', 'sub/3.txt']
114 >>> glob.glob('./**/', recursive=True)
115 ['./', './sub/']
Georg Brandl116aa622007-08-15 14:28:22 +0000116
Petri Lehtinenee4a20b2013-02-23 19:53:03 +0100117If the directory contains files starting with ``.`` they won't be matched by
118default. For example, consider a directory containing :file:`card.gif` and
119:file:`.card.gif`::
120
121 >>> import glob
122 >>> glob.glob('*.gif')
123 ['card.gif']
124 >>> glob.glob('.c*')
125 ['.card.gif']
Georg Brandl116aa622007-08-15 14:28:22 +0000126
127.. seealso::
128
129 Module :mod:`fnmatch`
130 Shell-style filename (not path) expansion
131