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 | |
| 10 | The :mod:`glob` module finds all the pathnames matching a specified pattern |
| 11 | according to the rules used by the Unix shell. No tilde expansion is done, but |
| 12 | ``*``, ``?``, and character ranges expressed with ``[]`` will be correctly |
| 13 | matched. This is done by using the :func:`os.listdir` and |
| 14 | :func:`fnmatch.fnmatch` functions in concert, and not by actually invoking a |
| 15 | subshell. (For tilde and shell variable expansion, use |
| 16 | :func:`os.path.expanduser` and :func:`os.path.expandvars`.) |
| 17 | |
Éric Araujo | 6e6cb8e | 2010-11-16 19:13:50 +0000 | [diff] [blame] | 18 | .. seealso:: |
| 19 | |
| 20 | Latest version of the :source:`glob module Python source code <Lib/glob.py>` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 21 | |
| 22 | .. function:: glob(pathname) |
| 23 | |
| 24 | Return a possibly-empty list of path names that match *pathname*, which must be |
| 25 | a string containing a path specification. *pathname* can be either absolute |
| 26 | (like :file:`/usr/src/Python-1.5/Makefile`) or relative (like |
| 27 | :file:`../../Tools/\*/\*.gif`), and can contain shell-style wildcards. Broken |
| 28 | symlinks are included in the results (as in the shell). |
| 29 | |
| 30 | |
| 31 | .. function:: iglob(pathname) |
| 32 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 33 | Return an :term:`iterator` which yields the same values as :func:`glob` |
| 34 | without actually storing them all simultaneously. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 35 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 36 | |
| 37 | For example, consider a directory containing only the following files: |
| 38 | :file:`1.gif`, :file:`2.txt`, and :file:`card.gif`. :func:`glob` will produce |
| 39 | the following results. Notice how any leading components of the path are |
| 40 | preserved. :: |
| 41 | |
| 42 | >>> import glob |
| 43 | >>> glob.glob('./[0-9].*') |
| 44 | ['./1.gif', './2.txt'] |
| 45 | >>> glob.glob('*.gif') |
| 46 | ['1.gif', 'card.gif'] |
| 47 | >>> glob.glob('?.gif') |
| 48 | ['1.gif'] |
| 49 | |
| 50 | |
| 51 | .. seealso:: |
| 52 | |
| 53 | Module :mod:`fnmatch` |
| 54 | Shell-style filename (not path) expansion |
| 55 | |