blob: 8c93e43e59cc623cfad88dd3286486ae339f199f [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Standard Module \module{glob}}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{standard}{glob}
3
4\modulesynopsis{\UNIX{} shell style pathname pattern expansion.}
5
Guido van Rossume6d579d1997-03-25 22:07:53 +00006
Fred Drakead511921998-02-16 21:25:53 +00007The \module{glob} module finds all the pathnames matching a specified
Guido van Rossume6d579d1997-03-25 22:07:53 +00008pattern according to the rules used by the \UNIX{} shell. No tilde
Fred Drake45c9df61997-12-29 15:55:10 +00009expansion is done, but \code{*}, \code{?}, and character ranges
10expressed with \code{[]} will be correctly matched. This is done by
Fred Drakead511921998-02-16 21:25:53 +000011using the \function{os.listdir()} and \function{fnmatch.fnmatch()}
12functions in concert, and not by actually invoking a subshell. (For
13tilde and shell variable expansion, use \function{os.path.expanduser()}
14and \function{os.path.expandvars()}.)
Guido van Rossume6d579d1997-03-25 22:07:53 +000015
16\begin{funcdesc}{glob}{pathname}
17Returns a possibly-empty list of path names that match \var{pathname},
18which must be a string containing a path specification.
19\var{pathname} can be either absolute (like
Fred Drake2de75ec1998-04-09 14:12:11 +000020\file{/usr/src/Python-1.5/Makefile}) or relative (like
Guido van Rossume6d579d1997-03-25 22:07:53 +000021\file{../../Tools/*.gif}), and can contain shell-style wildcards.
22\end{funcdesc}
23
24For example, consider a directory containing only the following files:
Fred Drake5bfe4851998-04-03 06:14:54 +000025\file{1.gif}, \file{2.txt}, and \file{card.gif}. \function{glob()}
Guido van Rossume6d579d1997-03-25 22:07:53 +000026will produce the following results. Notice how any leading components
27of the path are preserved.
28
Fred Drake19479911998-02-13 06:58:54 +000029\begin{verbatim}
Guido van Rossume6d579d1997-03-25 22:07:53 +000030>>> import glob
31>>> glob.glob('./[0-9].*')
32['./1.gif', './2.txt']
33>>> glob.glob('*.gif')
34['1.gif', 'card.gif']
35>>> glob.glob('?.gif')
36['1.gif']
Fred Drake19479911998-02-13 06:58:54 +000037\end{verbatim}