blob: eff138b3308e14b66a87666debd4095ed4161e21 [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
Raymond Hettinger4f707fd2011-01-10 19:54:11 +000012--------------
13
Georg Brandl116aa622007-08-15 14:28:22 +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
Petri Lehtinenee4a20b2013-02-23 19:53:03 +010019subshell. Note that unlike :func:`fnmatch.fnmatch`, :mod:`glob` treats
20filenames beginning with a dot (``.``) as special cases. (For tilde and shell
21variable expansion, use :func:`os.path.expanduser` and
22:func:`os.path.expandvars`.)
Georg Brandl116aa622007-08-15 14:28:22 +000023
Ezio Melottia39a22d2012-11-17 17:38:11 +020024For a literal match, wrap the meta-characters in brackets.
25For example, ``'[?]'`` matches the character ``'?'``.
26
Georg Brandl116aa622007-08-15 14:28:22 +000027
28.. function:: glob(pathname)
29
30 Return a possibly-empty list of path names that match *pathname*, which must be
31 a string containing a path specification. *pathname* can be either absolute
32 (like :file:`/usr/src/Python-1.5/Makefile`) or relative (like
33 :file:`../../Tools/\*/\*.gif`), and can contain shell-style wildcards. Broken
34 symlinks are included in the results (as in the shell).
35
36
37.. function:: iglob(pathname)
38
Georg Brandl9afde1c2007-11-01 20:32:30 +000039 Return an :term:`iterator` which yields the same values as :func:`glob`
40 without actually storing them all simultaneously.
Georg Brandl116aa622007-08-15 14:28:22 +000041
Georg Brandl116aa622007-08-15 14:28:22 +000042
43For example, consider a directory containing only the following files:
44:file:`1.gif`, :file:`2.txt`, and :file:`card.gif`. :func:`glob` will produce
45the following results. Notice how any leading components of the path are
46preserved. ::
47
48 >>> import glob
49 >>> glob.glob('./[0-9].*')
50 ['./1.gif', './2.txt']
51 >>> glob.glob('*.gif')
52 ['1.gif', 'card.gif']
53 >>> glob.glob('?.gif')
54 ['1.gif']
55
Petri Lehtinenee4a20b2013-02-23 19:53:03 +010056If the directory contains files starting with ``.`` they won't be matched by
57default. For example, consider a directory containing :file:`card.gif` and
58:file:`.card.gif`::
59
60 >>> import glob
61 >>> glob.glob('*.gif')
62 ['card.gif']
63 >>> glob.glob('.c*')
64 ['.card.gif']
Georg Brandl116aa622007-08-15 14:28:22 +000065
66.. seealso::
67
68 Module :mod:`fnmatch`
69 Shell-style filename (not path) expansion
70