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 | |
Serhiy Storchaka | 913876d | 2018-10-28 13:41:26 +0200 | [diff] [blame] | 13 | .. 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 21 | The :mod:`glob` module finds all the pathnames matching a specified pattern |
Martin Panter | 9f3c094 | 2015-11-16 23:46:22 +0000 | [diff] [blame] | 22 | according to the rules used by the Unix shell, although results are returned in |
| 23 | arbitrary order. No tilde expansion is done, but ``*``, ``?``, and character |
| 24 | ranges expressed with ``[]`` will be correctly matched. This is done by using |
Serhiy Storchaka | 28ab634 | 2016-09-06 22:33:41 +0300 | [diff] [blame] | 25 | the :func:`os.scandir` and :func:`fnmatch.fnmatch` functions in concert, and |
Martin Panter | 9f3c094 | 2015-11-16 23:46:22 +0000 | [diff] [blame] | 26 | not 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 Lehtinen | ee4a20b | 2013-02-23 19:53:03 +0100 | [diff] [blame] | 29 | :func:`os.path.expandvars`.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 30 | |
Ezio Melotti | a39a22d | 2012-11-17 17:38:11 +0200 | [diff] [blame] | 31 | For a literal match, wrap the meta-characters in brackets. |
| 32 | For example, ``'[?]'`` matches the character ``'?'``. |
| 33 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 34 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 35 | .. seealso:: |
| 36 | The :mod:`pathlib` module offers high-level path objects. |
| 37 | |
| 38 | |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 39 | .. function:: glob(pathname, *, recursive=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 40 | |
| 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 Oat | 52465e1 | 2018-11-04 06:50:55 -0800 | [diff] [blame] | 45 | symlinks are included in the results (as in the shell). Whether or not the |
| 46 | results are sorted depends on the file system. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 47 | |
Serhiy Storchaka | 913876d | 2018-10-28 13:41:26 +0200 | [diff] [blame] | 48 | .. index:: |
| 49 | single: **; in glob-style wildcards |
| 50 | |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 51 | If *recursive* is true, the pattern "``**``" will match any files and zero or |
Marc | e24594b | 2019-09-11 10:17:05 -0700 | [diff] [blame] | 52 | more directories, subdirectories and symbolic links to directories. If the |
| 53 | pattern is followed by an :data:`os.sep` or :data:`os.altsep` then files will not |
| 54 | match. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 55 | |
Steve Dower | 44f91c3 | 2019-06-27 10:47:59 -0700 | [diff] [blame] | 56 | .. audit-event:: glob.glob pathname,recursive glob.glob |
Steve Dower | 60419a7 | 2019-06-24 08:42:54 -0700 | [diff] [blame] | 57 | |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 58 | .. note:: |
| 59 | Using the "``**``" pattern in large directory trees may consume |
| 60 | an inordinate amount of time. |
| 61 | |
| 62 | .. versionchanged:: 3.5 |
| 63 | Support for recursive globs using "``**``". |
| 64 | |
| 65 | |
Zackery Spytz | 6887d86 | 2018-02-16 20:39:51 -0700 | [diff] [blame] | 66 | .. function:: iglob(pathname, *, recursive=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 67 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 68 | Return an :term:`iterator` which yields the same values as :func:`glob` |
| 69 | without actually storing them all simultaneously. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 70 | |
Steve Dower | 44f91c3 | 2019-06-27 10:47:59 -0700 | [diff] [blame] | 71 | .. audit-event:: glob.glob pathname,recursive glob.iglob |
Steve Dower | 60419a7 | 2019-06-24 08:42:54 -0700 | [diff] [blame] | 72 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 73 | |
Serhiy Storchaka | fd32fff | 2013-11-18 13:06:43 +0200 | [diff] [blame] | 74 | .. function:: escape(pathname) |
| 75 | |
| 76 | Escape all special characters (``'?'``, ``'*'`` and ``'['``). |
| 77 | This is useful if you want to match an arbitrary literal string that may |
| 78 | have special characters in it. Special characters in drive/UNC |
| 79 | sharepoints are not escaped, e.g. on Windows |
| 80 | ``escape('//?/c:/Quo vadis?.txt')`` returns ``'//?/c:/Quo vadis[?].txt'``. |
| 81 | |
| 82 | .. versionadded:: 3.4 |
| 83 | |
| 84 | |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 85 | For example, consider a directory containing the following files: |
| 86 | :file:`1.gif`, :file:`2.txt`, :file:`card.gif` and a subdirectory :file:`sub` |
| 87 | which contains only the file :file:`3.txt`. :func:`glob` will produce |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 88 | the following results. Notice how any leading components of the path are |
| 89 | preserved. :: |
| 90 | |
| 91 | >>> import glob |
| 92 | >>> glob.glob('./[0-9].*') |
| 93 | ['./1.gif', './2.txt'] |
| 94 | >>> glob.glob('*.gif') |
| 95 | ['1.gif', 'card.gif'] |
| 96 | >>> glob.glob('?.gif') |
| 97 | ['1.gif'] |
Serhiy Storchaka | c2edcdd | 2014-09-11 12:17:37 +0300 | [diff] [blame] | 98 | >>> glob.glob('**/*.txt', recursive=True) |
| 99 | ['2.txt', 'sub/3.txt'] |
| 100 | >>> glob.glob('./**/', recursive=True) |
| 101 | ['./', './sub/'] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 102 | |
Petri Lehtinen | ee4a20b | 2013-02-23 19:53:03 +0100 | [diff] [blame] | 103 | If the directory contains files starting with ``.`` they won't be matched by |
| 104 | default. For example, consider a directory containing :file:`card.gif` and |
| 105 | :file:`.card.gif`:: |
| 106 | |
| 107 | >>> import glob |
| 108 | >>> glob.glob('*.gif') |
| 109 | ['card.gif'] |
| 110 | >>> glob.glob('.c*') |
| 111 | ['.card.gif'] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 112 | |
| 113 | .. seealso:: |
| 114 | |
| 115 | Module :mod:`fnmatch` |
| 116 | Shell-style filename (not path) expansion |
| 117 | |