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