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