blob: f3f4fb7e55e705bc53f534b28adeb941e4ced6af [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}
Johannes Gijsbers836f5432005-01-08 13:13:19 +000019Return a possibly-empty list of path names that match \var{pathname},
Guido van Rossume6d579d1997-03-25 22:07:53 +000020which 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.
Johannes Gijsbersae882f72004-08-30 10:19:56 +000024Broken symlinks are included in the results (as in the shell).
Guido van Rossume6d579d1997-03-25 22:07:53 +000025\end{funcdesc}
26
Johannes Gijsbers836f5432005-01-08 13:13:19 +000027\begin{funcdesc}{iglob}{pathname}
28Return an iterator which yields the same values as \function{glob()}
29without actually storing them all simultaneously.
30\versionadded{2.5}
31\end{funcdesc}
32
Guido van Rossume6d579d1997-03-25 22:07:53 +000033For example, consider a directory containing only the following files:
Fred Drake5bfe4851998-04-03 06:14:54 +000034\file{1.gif}, \file{2.txt}, and \file{card.gif}. \function{glob()}
Guido van Rossume6d579d1997-03-25 22:07:53 +000035will produce the following results. Notice how any leading components
36of the path are preserved.
37
Fred Drake19479911998-02-13 06:58:54 +000038\begin{verbatim}
Guido van Rossume6d579d1997-03-25 22:07:53 +000039>>> import glob
40>>> glob.glob('./[0-9].*')
41['./1.gif', './2.txt']
42>>> glob.glob('*.gif')
43['1.gif', 'card.gif']
44>>> glob.glob('?.gif')
45['1.gif']
Fred Drake19479911998-02-13 06:58:54 +000046\end{verbatim}
Fred Drake8d2c0c21999-03-16 16:40:01 +000047
48
49\begin{seealso}
50 \seemodule{fnmatch}{Shell-style filename (not path) expansion}
51\end{seealso}