blob: 4c01bf6f1eeb4aae7ed58e19d6008978249d76c3 [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
7
8.. index:: single: filenames; pathname expansion
9
Raymond Hettinger10480942011-01-10 03:26:08 +000010**Source code:** :source:`Lib/glob.py`
11
Raymond Hettinger4f707fd2011-01-10 19:54:11 +000012--------------
13
Georg Brandl116aa622007-08-15 14:28:22 +000014The :mod:`glob` module finds all the pathnames matching a specified pattern
Martin Panter9f3c0942015-11-16 23:46:22 +000015according to the rules used by the Unix shell, although results are returned in
16arbitrary order. No tilde expansion is done, but ``*``, ``?``, and character
17ranges expressed with ``[]`` will be correctly matched. This is done by using
18the :func:`os.listdir` and :func:`fnmatch.fnmatch` functions in concert, and
19not by actually invoking a subshell. Note that unlike :func:`fnmatch.fnmatch`,
20:mod:`glob` treats filenames beginning with a dot (``.``) as special cases.
21(For tilde and shell variable expansion, use :func:`os.path.expanduser` and
Petri Lehtinenee4a20b2013-02-23 19:53:03 +010022:func:`os.path.expandvars`.)
Georg Brandl116aa622007-08-15 14:28:22 +000023
Ezio Melottia39a22d2012-11-17 17:38:11 +020024For a literal match, wrap the meta-characters in brackets.
25For example, ``'[?]'`` matches the character ``'?'``.
26
Georg Brandl116aa622007-08-15 14:28:22 +000027
Antoine Pitrou31119e42013-11-22 17:38:12 +010028.. seealso::
29 The :mod:`pathlib` module offers high-level path objects.
30
31
Serhiy Storchakac2edcdd2014-09-11 12:17:37 +030032.. function:: glob(pathname, *, recursive=False)
Georg Brandl116aa622007-08-15 14:28:22 +000033
34 Return a possibly-empty list of path names that match *pathname*, which must be
35 a string containing a path specification. *pathname* can be either absolute
36 (like :file:`/usr/src/Python-1.5/Makefile`) or relative (like
37 :file:`../../Tools/\*/\*.gif`), and can contain shell-style wildcards. Broken
38 symlinks are included in the results (as in the shell).
39
Serhiy Storchakac2edcdd2014-09-11 12:17:37 +030040 If *recursive* is true, the pattern "``**``" will match any files and zero or
Serhiy Storchakaf51d7152015-11-02 14:40:41 +020041 more directories and subdirectories. If the pattern is followed by an
Serhiy Storchakac2edcdd2014-09-11 12:17:37 +030042 ``os.sep``, only directories and subdirectories match.
Georg Brandl116aa622007-08-15 14:28:22 +000043
Serhiy Storchakac2edcdd2014-09-11 12:17:37 +030044 .. note::
45 Using the "``**``" pattern in large directory trees may consume
46 an inordinate amount of time.
47
48 .. versionchanged:: 3.5
49 Support for recursive globs using "``**``".
50
51
52.. function:: iglob(pathname, recursive=False)
Georg Brandl116aa622007-08-15 14:28:22 +000053
Georg Brandl9afde1c2007-11-01 20:32:30 +000054 Return an :term:`iterator` which yields the same values as :func:`glob`
55 without actually storing them all simultaneously.
Georg Brandl116aa622007-08-15 14:28:22 +000056
Georg Brandl116aa622007-08-15 14:28:22 +000057
Serhiy Storchakafd32fff2013-11-18 13:06:43 +020058.. function:: escape(pathname)
59
60 Escape all special characters (``'?'``, ``'*'`` and ``'['``).
61 This is useful if you want to match an arbitrary literal string that may
62 have special characters in it. Special characters in drive/UNC
63 sharepoints are not escaped, e.g. on Windows
64 ``escape('//?/c:/Quo vadis?.txt')`` returns ``'//?/c:/Quo vadis[?].txt'``.
65
66 .. versionadded:: 3.4
67
68
Serhiy Storchakac2edcdd2014-09-11 12:17:37 +030069For example, consider a directory containing the following files:
70:file:`1.gif`, :file:`2.txt`, :file:`card.gif` and a subdirectory :file:`sub`
71which contains only the file :file:`3.txt`. :func:`glob` will produce
Georg Brandl116aa622007-08-15 14:28:22 +000072the following results. Notice how any leading components of the path are
73preserved. ::
74
75 >>> import glob
76 >>> glob.glob('./[0-9].*')
77 ['./1.gif', './2.txt']
78 >>> glob.glob('*.gif')
79 ['1.gif', 'card.gif']
80 >>> glob.glob('?.gif')
81 ['1.gif']
Serhiy Storchakac2edcdd2014-09-11 12:17:37 +030082 >>> glob.glob('**/*.txt', recursive=True)
83 ['2.txt', 'sub/3.txt']
84 >>> glob.glob('./**/', recursive=True)
85 ['./', './sub/']
Georg Brandl116aa622007-08-15 14:28:22 +000086
Petri Lehtinenee4a20b2013-02-23 19:53:03 +010087If the directory contains files starting with ``.`` they won't be matched by
88default. For example, consider a directory containing :file:`card.gif` and
89:file:`.card.gif`::
90
91 >>> import glob
92 >>> glob.glob('*.gif')
93 ['card.gif']
94 >>> glob.glob('.c*')
95 ['.card.gif']
Georg Brandl116aa622007-08-15 14:28:22 +000096
97.. seealso::
98
99 Module :mod:`fnmatch`
100 Shell-style filename (not path) expansion
101