[2] | 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 |
|
---|
[391] | 10 | **Source code:** :source:`Lib/glob.py`
|
---|
| 11 |
|
---|
| 12 | --------------
|
---|
| 13 |
|
---|
[2] | 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
|
---|
[391] | 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`.)
|
---|
[2] | 23 |
|
---|
[391] | 24 | For a literal match, wrap the meta-characters in brackets.
|
---|
| 25 | For example, ``'[?]'`` matches the character ``'?'``.
|
---|
[2] | 26 |
|
---|
[391] | 27 |
|
---|
[2] | 28 | .. function:: glob(pathname)
|
---|
| 29 |
|
---|
| 30 | Return a possibly-empty list of path names that match *pathname*, which must be
|
---|
| 31 | a string containing a path specification. *pathname* can be either absolute
|
---|
| 32 | (like :file:`/usr/src/Python-1.5/Makefile`) or relative (like
|
---|
| 33 | :file:`../../Tools/\*/\*.gif`), and can contain shell-style wildcards. Broken
|
---|
| 34 | symlinks are included in the results (as in the shell).
|
---|
| 35 |
|
---|
| 36 |
|
---|
| 37 | .. function:: iglob(pathname)
|
---|
| 38 |
|
---|
| 39 | Return an :term:`iterator` which yields the same values as :func:`glob`
|
---|
| 40 | without actually storing them all simultaneously.
|
---|
| 41 |
|
---|
| 42 | .. versionadded:: 2.5
|
---|
| 43 |
|
---|
| 44 | For example, consider a directory containing only the following files:
|
---|
| 45 | :file:`1.gif`, :file:`2.txt`, and :file:`card.gif`. :func:`glob` will produce
|
---|
| 46 | the following results. Notice how any leading components of the path are
|
---|
| 47 | preserved. ::
|
---|
| 48 |
|
---|
| 49 | >>> import glob
|
---|
| 50 | >>> glob.glob('./[0-9].*')
|
---|
| 51 | ['./1.gif', './2.txt']
|
---|
| 52 | >>> glob.glob('*.gif')
|
---|
| 53 | ['1.gif', 'card.gif']
|
---|
| 54 | >>> glob.glob('?.gif')
|
---|
| 55 | ['1.gif']
|
---|
| 56 |
|
---|
[391] | 57 | If the directory contains files starting with ``.`` they won't be matched by
|
---|
| 58 | default. For example, consider a directory containing :file:`card.gif` and
|
---|
| 59 | :file:`.card.gif`::
|
---|
[2] | 60 |
|
---|
[391] | 61 | >>> import glob
|
---|
| 62 | >>> glob.glob('*.gif')
|
---|
| 63 | ['card.gif']
|
---|
| 64 | >>> glob.glob('.c*')
|
---|
| 65 | ['.card.gif']
|
---|
| 66 |
|
---|
[2] | 67 | .. seealso::
|
---|
| 68 |
|
---|
| 69 | Module :mod:`fnmatch`
|
---|
| 70 | Shell-style filename (not path) expansion
|
---|
| 71 |
|
---|