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