blob: dbc6b6acc0cad1c79555b8c517bf733eafda9f9c [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}
Fred Drake19479911998-02-13 06:58:54 +00004\setindexsubitem{(in module glob)}
Guido van Rossume6d579d1997-03-25 22:07:53 +00005
Fred Drakead511921998-02-16 21:25:53 +00006The \module{glob} module finds all the pathnames matching a specified
Guido van Rossume6d579d1997-03-25 22:07:53 +00007pattern according to the rules used by the \UNIX{} shell. No tilde
Fred Drake45c9df61997-12-29 15:55:10 +00008expansion is done, but \code{*}, \code{?}, and character ranges
9expressed with \code{[]} will be correctly matched. This is done by
Fred Drakead511921998-02-16 21:25:53 +000010using the \function{os.listdir()} and \function{fnmatch.fnmatch()}
11functions in concert, and not by actually invoking a subshell. (For
12tilde and shell variable expansion, use \function{os.path.expanduser()}
13and \function{os.path.expandvars()}.)
Guido van Rossume6d579d1997-03-25 22:07:53 +000014
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
Fred Drakead511921998-02-16 21:25:53 +000019\file{/usr/src/Python\version/Makefile}) or relative (like
Guido van Rossume6d579d1997-03-25 22:07:53 +000020\file{../../Tools/*.gif}), and can contain shell-style wildcards.
21\end{funcdesc}
22
23For example, consider a directory containing only the following files:
Fred Drakead511921998-02-16 21:25:53 +000024\file{1.gif}, \file{2.txt}, and \file{card.gif}. \function{glob.glob()}
Guido van Rossume6d579d1997-03-25 22:07:53 +000025will produce the following results. Notice how any leading components
26of the path are preserved.
27
Fred Drake19479911998-02-13 06:58:54 +000028\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']
Fred Drake19479911998-02-13 06:58:54 +000036\end{verbatim}