source: vendor/python/2.5/Doc/lib/libglob.tex

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 1.8 KB
Line 
1\section{\module{glob} ---
2 \UNIX{} style pathname pattern expansion}
3
4\declaremodule{standard}{glob}
5\modulesynopsis{\UNIX\ shell style pathname pattern expansion.}
6
7
8The \module{glob} module finds all the pathnames matching a specified
9pattern according to the rules used by the \UNIX{} shell. No tilde
10expansion is done, but \code{*}, \code{?}, and character ranges
11expressed with \code{[]} will be correctly matched. This is done by
12using the \function{os.listdir()} and \function{fnmatch.fnmatch()}
13functions in concert, and not by actually invoking a subshell. (For
14tilde and shell variable expansion, use \function{os.path.expanduser()}
15and \function{os.path.expandvars()}.)
16\index{filenames!pathname expansion}
17
18\begin{funcdesc}{glob}{pathname}
19Return a possibly-empty list of path names that match \var{pathname},
20which must be a string containing a path specification.
21\var{pathname} can be either absolute (like
22\file{/usr/src/Python-1.5/Makefile}) or relative (like
23\file{../../Tools/*/*.gif}), and can contain shell-style wildcards.
24Broken symlinks are included in the results (as in the shell).
25\end{funcdesc}
26
27\begin{funcdesc}{iglob}{pathname}
28Return an iterator which yields the same values as \function{glob()}
29without actually storing them all simultaneously.
30\versionadded{2.5}
31\end{funcdesc}
32
33For example, consider a directory containing only the following files:
34\file{1.gif}, \file{2.txt}, and \file{card.gif}. \function{glob()}
35will produce the following results. Notice how any leading components
36of the path are preserved.
37
38\begin{verbatim}
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\end{verbatim}
47
48
49\begin{seealso}
50 \seemodule{fnmatch}{Shell-style filename (not path) expansion}
51\end{seealso}
Note: See TracBrowser for help on using the repository browser.