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