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