blob: 1ef9a310615fd180b973c5a24a80bdfc0477ca15 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +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
Raymond Hettingere0e08222010-11-06 07:10:31 +000019.. 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 Brandl8ec7f652007-08-15 14:28:01 +000023
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 Brandle7a09902007-10-21 12:10:28 +000035 Return an :term:`iterator` which yields the same values as :func:`glob`
36 without actually storing them all simultaneously.
Georg Brandl8ec7f652007-08-15 14:28:01 +000037
38 .. versionadded:: 2.5
39
40For example, consider a directory containing only the following files:
41:file:`1.gif`, :file:`2.txt`, and :file:`card.gif`. :func:`glob` will produce
42the following results. Notice how any leading components of the path are
43preserved. ::
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