blob: 0db10b5efc10cb9d8b0253c84bb12999cc7900e1 [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 Storchakac2edcdd2014-09-11 12:17:37 +030039.. function:: glob(pathname, *, 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
45 symlinks are included in the results (as in the shell).
46
Serhiy Storchaka913876d2018-10-28 13:41:26 +020047 .. index::
48 single: **; in glob-style wildcards
49
Serhiy Storchakac2edcdd2014-09-11 12:17:37 +030050 If *recursive* is true, the pattern "``**``" will match any files and zero or
Serhiy Storchakaf51d7152015-11-02 14:40:41 +020051 more directories and subdirectories. If the pattern is followed by an
Serhiy Storchakac2edcdd2014-09-11 12:17:37 +030052 ``os.sep``, only directories and subdirectories match.
Georg Brandl116aa622007-08-15 14:28:22 +000053
Serhiy Storchakac2edcdd2014-09-11 12:17:37 +030054 .. note::
55 Using the "``**``" pattern in large directory trees may consume
56 an inordinate amount of time.
57
58 .. versionchanged:: 3.5
59 Support for recursive globs using "``**``".
60
61
Zackery Spytz6887d862018-02-16 20:39:51 -070062.. function:: iglob(pathname, *, recursive=False)
Georg Brandl116aa622007-08-15 14:28:22 +000063
Georg Brandl9afde1c2007-11-01 20:32:30 +000064 Return an :term:`iterator` which yields the same values as :func:`glob`
65 without actually storing them all simultaneously.
Georg Brandl116aa622007-08-15 14:28:22 +000066
Georg Brandl116aa622007-08-15 14:28:22 +000067
Serhiy Storchakafd32fff2013-11-18 13:06:43 +020068.. function:: escape(pathname)
69
70 Escape all special characters (``'?'``, ``'*'`` and ``'['``).
71 This is useful if you want to match an arbitrary literal string that may
72 have special characters in it. Special characters in drive/UNC
73 sharepoints are not escaped, e.g. on Windows
74 ``escape('//?/c:/Quo vadis?.txt')`` returns ``'//?/c:/Quo vadis[?].txt'``.
75
76 .. versionadded:: 3.4
77
78
Serhiy Storchakac2edcdd2014-09-11 12:17:37 +030079For example, consider a directory containing the following files:
80:file:`1.gif`, :file:`2.txt`, :file:`card.gif` and a subdirectory :file:`sub`
81which contains only the file :file:`3.txt`. :func:`glob` will produce
Georg Brandl116aa622007-08-15 14:28:22 +000082the following results. Notice how any leading components of the path are
83preserved. ::
84
85 >>> import glob
86 >>> glob.glob('./[0-9].*')
87 ['./1.gif', './2.txt']
88 >>> glob.glob('*.gif')
89 ['1.gif', 'card.gif']
90 >>> glob.glob('?.gif')
91 ['1.gif']
Serhiy Storchakac2edcdd2014-09-11 12:17:37 +030092 >>> glob.glob('**/*.txt', recursive=True)
93 ['2.txt', 'sub/3.txt']
94 >>> glob.glob('./**/', recursive=True)
95 ['./', './sub/']
Georg Brandl116aa622007-08-15 14:28:22 +000096
Petri Lehtinenee4a20b2013-02-23 19:53:03 +010097If the directory contains files starting with ``.`` they won't be matched by
98default. For example, consider a directory containing :file:`card.gif` and
99:file:`.card.gif`::
100
101 >>> import glob
102 >>> glob.glob('*.gif')
103 ['card.gif']
104 >>> glob.glob('.c*')
105 ['.card.gif']
Georg Brandl116aa622007-08-15 14:28:22 +0000106
107.. seealso::
108
109 Module :mod:`fnmatch`
110 Shell-style filename (not path) expansion
111