blob: 5ef06f3723c6a9e7a9eb8d344b922d7971d9fb69 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{glob} ---
2 \UNIX{} shell style pathname pattern expansion.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{glob}
4
5\modulesynopsis{\UNIX{} shell style pathname pattern expansion.}
6
Guido van Rossume6d579d1997-03-25 22:07:53 +00007
Fred Drakead511921998-02-16 21:25:53 +00008The \module{glob} module finds all the pathnames matching a specified
Guido van Rossume6d579d1997-03-25 22:07:53 +00009pattern according to the rules used by the \UNIX{} shell. No tilde
Fred Drake45c9df61997-12-29 15:55:10 +000010expansion is done, but \code{*}, \code{?}, and character ranges
11expressed with \code{[]} will be correctly matched. This is done by
Fred Drakead511921998-02-16 21:25:53 +000012using 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()}.)
Guido van Rossume6d579d1997-03-25 22:07:53 +000016
17\begin{funcdesc}{glob}{pathname}
18Returns a possibly-empty list of path names that match \var{pathname},
19which must be a string containing a path specification.
20\var{pathname} can be either absolute (like
Fred Drake2de75ec1998-04-09 14:12:11 +000021\file{/usr/src/Python-1.5/Makefile}) or relative (like
Guido van Rossume6d579d1997-03-25 22:07:53 +000022\file{../../Tools/*.gif}), and can contain shell-style wildcards.
23\end{funcdesc}
24
25For example, consider a directory containing only the following files:
Fred Drake5bfe4851998-04-03 06:14:54 +000026\file{1.gif}, \file{2.txt}, and \file{card.gif}. \function{glob()}
Guido van Rossume6d579d1997-03-25 22:07:53 +000027will produce the following results. Notice how any leading components
28of the path are preserved.
29
Fred Drake19479911998-02-13 06:58:54 +000030\begin{verbatim}
Guido van Rossume6d579d1997-03-25 22:07:53 +000031>>> import glob
32>>> glob.glob('./[0-9].*')
33['./1.gif', './2.txt']
34>>> glob.glob('*.gif')
35['1.gif', 'card.gif']
36>>> glob.glob('?.gif')
37['1.gif']
Fred Drake19479911998-02-13 06:58:54 +000038\end{verbatim}