blob: 25bd4b7f9a874c6136825a4703d9a82568837fa5 [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
Georg Brandl116aa622007-08-15 14:28:22 +000013The :mod:`glob` module finds all the pathnames matching a specified pattern
Martin Panter9f3c0942015-11-16 23:46:22 +000014according to the rules used by the Unix shell, although results are returned in
15arbitrary order. No tilde expansion is done, but ``*``, ``?``, and character
16ranges expressed with ``[]`` will be correctly matched. This is done by using
Serhiy Storchaka28ab6342016-09-06 22:33:41 +030017the :func:`os.scandir` and :func:`fnmatch.fnmatch` functions in concert, and
Martin Panter9f3c0942015-11-16 23:46:22 +000018not by actually invoking a subshell. Note that unlike :func:`fnmatch.fnmatch`,
19:mod:`glob` treats filenames beginning with a dot (``.``) as special cases.
20(For tilde and shell variable expansion, use :func:`os.path.expanduser` and
Petri Lehtinenee4a20b2013-02-23 19:53:03 +010021:func:`os.path.expandvars`.)
Georg Brandl116aa622007-08-15 14:28:22 +000022
Ezio Melottia39a22d2012-11-17 17:38:11 +020023For a literal match, wrap the meta-characters in brackets.
24For example, ``'[?]'`` matches the character ``'?'``.
25
Georg Brandl116aa622007-08-15 14:28:22 +000026
Antoine Pitrou31119e42013-11-22 17:38:12 +010027.. seealso::
28 The :mod:`pathlib` module offers high-level path objects.
29
30
Serhiy Storchakac2edcdd2014-09-11 12:17:37 +030031.. function:: glob(pathname, *, recursive=False)
Georg Brandl116aa622007-08-15 14:28:22 +000032
33 Return a possibly-empty list of path names that match *pathname*, which must be
34 a string containing a path specification. *pathname* can be either absolute
35 (like :file:`/usr/src/Python-1.5/Makefile`) or relative (like
36 :file:`../../Tools/\*/\*.gif`), and can contain shell-style wildcards. Broken
37 symlinks are included in the results (as in the shell).
38
Serhiy Storchakac2edcdd2014-09-11 12:17:37 +030039 If *recursive* is true, the pattern "``**``" will match any files and zero or
Serhiy Storchakaf51d7152015-11-02 14:40:41 +020040 more directories and subdirectories. If the pattern is followed by an
Serhiy Storchakac2edcdd2014-09-11 12:17:37 +030041 ``os.sep``, only directories and subdirectories match.
Georg Brandl116aa622007-08-15 14:28:22 +000042
Serhiy Storchakac2edcdd2014-09-11 12:17:37 +030043 .. note::
44 Using the "``**``" pattern in large directory trees may consume
45 an inordinate amount of time.
46
47 .. versionchanged:: 3.5
48 Support for recursive globs using "``**``".
49
50
Zackery Spytz6887d862018-02-16 20:39:51 -070051.. function:: iglob(pathname, *, recursive=False)
Georg Brandl116aa622007-08-15 14:28:22 +000052
Georg Brandl9afde1c2007-11-01 20:32:30 +000053 Return an :term:`iterator` which yields the same values as :func:`glob`
54 without actually storing them all simultaneously.
Georg Brandl116aa622007-08-15 14:28:22 +000055
Georg Brandl116aa622007-08-15 14:28:22 +000056
Serhiy Storchakafd32fff2013-11-18 13:06:43 +020057.. function:: escape(pathname)
58
59 Escape all special characters (``'?'``, ``'*'`` and ``'['``).
60 This is useful if you want to match an arbitrary literal string that may
61 have special characters in it. Special characters in drive/UNC
62 sharepoints are not escaped, e.g. on Windows
63 ``escape('//?/c:/Quo vadis?.txt')`` returns ``'//?/c:/Quo vadis[?].txt'``.
64
65 .. versionadded:: 3.4
66
67
Serhiy Storchakac2edcdd2014-09-11 12:17:37 +030068For example, consider a directory containing the following files:
69:file:`1.gif`, :file:`2.txt`, :file:`card.gif` and a subdirectory :file:`sub`
70which contains only the file :file:`3.txt`. :func:`glob` will produce
Georg Brandl116aa622007-08-15 14:28:22 +000071the following results. Notice how any leading components of the path are
72preserved. ::
73
74 >>> import glob
75 >>> glob.glob('./[0-9].*')
76 ['./1.gif', './2.txt']
77 >>> glob.glob('*.gif')
78 ['1.gif', 'card.gif']
79 >>> glob.glob('?.gif')
80 ['1.gif']
Serhiy Storchakac2edcdd2014-09-11 12:17:37 +030081 >>> glob.glob('**/*.txt', recursive=True)
82 ['2.txt', 'sub/3.txt']
83 >>> glob.glob('./**/', recursive=True)
84 ['./', './sub/']
Georg Brandl116aa622007-08-15 14:28:22 +000085
Petri Lehtinenee4a20b2013-02-23 19:53:03 +010086If the directory contains files starting with ``.`` they won't be matched by
87default. For example, consider a directory containing :file:`card.gif` and
88:file:`.card.gif`::
89
90 >>> import glob
91 >>> glob.glob('*.gif')
92 ['card.gif']
93 >>> glob.glob('.c*')
94 ['.card.gif']
Georg Brandl116aa622007-08-15 14:28:22 +000095
96.. seealso::
97
98 Module :mod:`fnmatch`
99 Shell-style filename (not path) expansion
100