blob: 142afd80210863f9caea45b769e8d467c5de863f [file] [log] [blame]
Guido van Rossume6d579d1997-03-25 22:07:53 +00001\section{Standard Module \sectcode{glob}}
2\stmodindex{glob}
3\renewcommand{\indexsubitem}{(in module glob)}
4
5The \code{glob} module finds all the pathnames matching a specified
6pattern according to the rules used by the \UNIX{} shell. No tilde
7expansion is done, but \verb\*\, \verb\?\, and character ranges
8expressed with \verb\[]\ will be correctly matched. This is done by
9using the \code{os.listdir()} and \code{fnmatch.fnmatch()} functions
10in concert, and not by actually invoking a subshell. (For tilde and
11shell variable expansion, use \code{os.path.expanduser(}) and
12\code{os.path.expandvars()}.)
13
14\begin{funcdesc}{glob}{pathname}
15Returns a possibly-empty list of path names that match \var{pathname},
16which must be a string containing a path specification.
17\var{pathname} can be either absolute (like
18\file{/usr/src/Python1.4/Makefile}) or relative (like
19\file{../../Tools/*.gif}), and can contain shell-style wildcards.
20\end{funcdesc}
21
22For example, consider a directory containing only the following files:
23\file{1.gif}, \file{2.txt}, and \file{card.gif}. \code{glob.glob()}
24will produce the following results. Notice how any leading components
25of the path are preserved.
26
27\begin{verbatim}
28>>> import glob
29>>> glob.glob('./[0-9].*')
30['./1.gif', './2.txt']
31>>> glob.glob('*.gif')
32['1.gif', 'card.gif']
33>>> glob.glob('?.gif')
34['1.gif']
35\end{verbatim}