Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`glob` --- Unix style pathname pattern expansion |
| 2 | ===================================================== |
| 3 | |
| 4 | .. module:: glob |
| 5 | :synopsis: Unix shell style pathname pattern expansion. |
| 6 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 7 | **Source code:** :source:`Lib/glob.py` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 8 | |
| 9 | .. index:: single: filenames; pathname expansion |
| 10 | |
Raymond Hettinger | 4f707fd | 2011-01-10 19:54:11 +0000 | [diff] [blame] | 11 | -------------- |
| 12 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 | The :mod:`glob` module finds all the pathnames matching a specified pattern |
Martin Panter | 9f3c094 | 2015-11-16 23:46:22 +0000 | [diff] [blame] | 14 | according to the rules used by the Unix shell, although results are returned in |
| 15 | arbitrary order. No tilde expansion is done, but ``*``, ``?``, and character |
| 16 | ranges expressed with ``[]`` will be correctly matched. This is done by using |
Serhiy Storchaka | 28ab634 | 2016-09-06 22:33:41 +0300 | [diff] [blame] | 17 | the :func:`os.scandir` and :func:`fnmatch.fnmatch` functions in concert, and |
Martin Panter | 9f3c094 | 2015-11-16 23:46:22 +0000 | [diff] [blame] | 18 | not 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 Lehtinen | ee4a20b | 2013-02-23 19:53:03 +0100 | [diff] [blame] | 21 | :func:`os.path.expandvars`.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 22 | |
Ezio Melotti | a39a22d | 2012-11-17 17:38:11 +0200 | [diff] [blame] | 23 | For a literal match, wrap the meta-characters in brackets. |
| 24 | For example, ``'[?]'`` matches the character ``'?'``. |
| 25 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 26 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 27 | .. seealso:: |
| 28 | The :mod:`pathlib` module offers high-level path objects. |
| 29 | |
| 30 | |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 31 | .. function:: glob(pathname, *, recursive=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 32 | |
| 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 Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 39 | If *recursive* is true, the pattern "``**``" will match any files and zero or |
Serhiy Storchaka | f51d715 | 2015-11-02 14:40:41 +0200 | [diff] [blame] | 40 | more directories and subdirectories. If the pattern is followed by an |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 41 | ``os.sep``, only directories and subdirectories match. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 42 | |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 43 | .. 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 Spytz | 6887d86 | 2018-02-16 20:39:51 -0700 | [diff] [blame] | 51 | .. function:: iglob(pathname, *, recursive=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 52 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 53 | Return an :term:`iterator` which yields the same values as :func:`glob` |
| 54 | without actually storing them all simultaneously. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 55 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 56 | |
Serhiy Storchaka | fd32fff | 2013-11-18 13:06:43 +0200 | [diff] [blame] | 57 | .. 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 Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 68 | For example, consider a directory containing the following files: |
| 69 | :file:`1.gif`, :file:`2.txt`, :file:`card.gif` and a subdirectory :file:`sub` |
| 70 | which contains only the file :file:`3.txt`. :func:`glob` will produce |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 71 | the following results. Notice how any leading components of the path are |
| 72 | preserved. :: |
| 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 Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 81 | >>> glob.glob('**/*.txt', recursive=True) |
| 82 | ['2.txt', 'sub/3.txt'] |
| 83 | >>> glob.glob('./**/', recursive=True) |
| 84 | ['./', './sub/'] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 85 | |
Petri Lehtinen | ee4a20b | 2013-02-23 19:53:03 +0100 | [diff] [blame] | 86 | If the directory contains files starting with ``.`` they won't be matched by |
| 87 | default. 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 95 | |
| 96 | .. seealso:: |
| 97 | |
| 98 | Module :mod:`fnmatch` |
| 99 | Shell-style filename (not path) expansion |
| 100 | |