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