blob: 5c1de807bb168661f7ba6b149e1d50da97164cf0 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{glob} ---
Fred Drake8d2c0c21999-03-16 16:40:01 +00002 \UNIX{} style pathname pattern expansion}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drake8d2c0c21999-03-16 16:40:01 +00004\declaremodule{standard}{glob}
Fred Drake17a2b642000-10-09 18:12:29 +00005\modulesynopsis{\UNIX\ shell style pathname pattern expansion.}
Fred Drakeb91e9341998-07-23 17:59:49 +00006
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()}.)
Fred Drake8d2c0c21999-03-16 16:40:01 +000016\index{filenames!pathname expansion}
Guido van Rossume6d579d1997-03-25 22:07:53 +000017
18\begin{funcdesc}{glob}{pathname}
19Returns a possibly-empty list of path names that match \var{pathname},
20which must be a string containing a path specification.
21\var{pathname} can be either absolute (like
Fred Drake2de75ec1998-04-09 14:12:11 +000022\file{/usr/src/Python-1.5/Makefile}) or relative (like
Fred Drake8d2c0c21999-03-16 16:40:01 +000023\file{../../Tools/*/*.gif}), and can contain shell-style wildcards.
Guido van Rossume6d579d1997-03-25 22:07:53 +000024\end{funcdesc}
25
26For example, consider a directory containing only the following files:
Fred Drake5bfe4851998-04-03 06:14:54 +000027\file{1.gif}, \file{2.txt}, and \file{card.gif}. \function{glob()}
Guido van Rossume6d579d1997-03-25 22:07:53 +000028will produce the following results. Notice how any leading components
29of the path are preserved.
30
Fred Drake19479911998-02-13 06:58:54 +000031\begin{verbatim}
Guido van Rossume6d579d1997-03-25 22:07:53 +000032>>> import glob
33>>> glob.glob('./[0-9].*')
34['./1.gif', './2.txt']
35>>> glob.glob('*.gif')
36['1.gif', 'card.gif']
37>>> glob.glob('?.gif')
38['1.gif']
Fred Drake19479911998-02-13 06:58:54 +000039\end{verbatim}
Fred Drake8d2c0c21999-03-16 16:40:01 +000040
41
42\begin{seealso}
43 \seemodule{fnmatch}{Shell-style filename (not path) expansion}
44\end{seealso}