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 | |
| 7 | |
| 8 | .. index:: single: filenames; pathname expansion |
| 9 | |
Raymond Hettinger | 1048094 | 2011-01-10 03:26:08 +0000 | [diff] [blame] | 10 | **Source code:** :source:`Lib/glob.py` |
| 11 | |
Raymond Hettinger | 4f707fd | 2011-01-10 19:54:11 +0000 | [diff] [blame] | 12 | -------------- |
| 13 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 14 | The :mod:`glob` module finds all the pathnames matching a specified pattern |
| 15 | according to the rules used by the Unix shell. No tilde expansion is done, but |
| 16 | ``*``, ``?``, and character ranges expressed with ``[]`` will be correctly |
| 17 | matched. This is done by using the :func:`os.listdir` and |
| 18 | :func:`fnmatch.fnmatch` functions in concert, and not by actually invoking a |
Petri Lehtinen | ee4a20b | 2013-02-23 19:53:03 +0100 | [diff] [blame] | 19 | subshell. Note that unlike :func:`fnmatch.fnmatch`, :mod:`glob` treats |
| 20 | filenames beginning with a dot (``.``) as special cases. (For tilde and shell |
| 21 | variable expansion, use :func:`os.path.expanduser` and |
| 22 | :func:`os.path.expandvars`.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 23 | |
Ezio Melotti | a39a22d | 2012-11-17 17:38:11 +0200 | [diff] [blame] | 24 | For a literal match, wrap the meta-characters in brackets. |
| 25 | For example, ``'[?]'`` matches the character ``'?'``. |
| 26 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 27 | |
Antoine Pitrou | 31119e4 | 2013-11-22 17:38:12 +0100 | [diff] [blame] | 28 | .. seealso:: |
| 29 | The :mod:`pathlib` module offers high-level path objects. |
| 30 | |
| 31 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 32 | .. function:: glob(pathname) |
| 33 | |
| 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 | |
| 40 | |
| 41 | .. function:: iglob(pathname) |
| 42 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 43 | Return an :term:`iterator` which yields the same values as :func:`glob` |
| 44 | without actually storing them all simultaneously. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 45 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 46 | |
Serhiy Storchaka | fd32fff | 2013-11-18 13:06:43 +0200 | [diff] [blame] | 47 | .. function:: escape(pathname) |
| 48 | |
| 49 | Escape all special characters (``'?'``, ``'*'`` and ``'['``). |
| 50 | This is useful if you want to match an arbitrary literal string that may |
| 51 | have special characters in it. Special characters in drive/UNC |
| 52 | sharepoints are not escaped, e.g. on Windows |
| 53 | ``escape('//?/c:/Quo vadis?.txt')`` returns ``'//?/c:/Quo vadis[?].txt'``. |
| 54 | |
| 55 | .. versionadded:: 3.4 |
| 56 | |
| 57 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 58 | For example, consider a directory containing only the following files: |
| 59 | :file:`1.gif`, :file:`2.txt`, and :file:`card.gif`. :func:`glob` will produce |
| 60 | the following results. Notice how any leading components of the path are |
| 61 | preserved. :: |
| 62 | |
| 63 | >>> import glob |
| 64 | >>> glob.glob('./[0-9].*') |
| 65 | ['./1.gif', './2.txt'] |
| 66 | >>> glob.glob('*.gif') |
| 67 | ['1.gif', 'card.gif'] |
| 68 | >>> glob.glob('?.gif') |
| 69 | ['1.gif'] |
| 70 | |
Petri Lehtinen | ee4a20b | 2013-02-23 19:53:03 +0100 | [diff] [blame] | 71 | If the directory contains files starting with ``.`` they won't be matched by |
| 72 | default. For example, consider a directory containing :file:`card.gif` and |
| 73 | :file:`.card.gif`:: |
| 74 | |
| 75 | >>> import glob |
| 76 | >>> glob.glob('*.gif') |
| 77 | ['card.gif'] |
| 78 | >>> glob.glob('.c*') |
| 79 | ['.card.gif'] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 80 | |
| 81 | .. seealso:: |
| 82 | |
| 83 | Module :mod:`fnmatch` |
| 84 | Shell-style filename (not path) expansion |
| 85 | |