blob: 10f667ad69bec73f0610c5884cb06aa4e1b26955 [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Standard Module \module{glob}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-glob}
Guido van Rossume6d579d1997-03-25 22:07:53 +00003\stmodindex{glob}
Guido van Rossume6d579d1997-03-25 22:07:53 +00004
Fred Drakead511921998-02-16 21:25:53 +00005The \module{glob} module finds all the pathnames matching a specified
Guido van Rossume6d579d1997-03-25 22:07:53 +00006pattern according to the rules used by the \UNIX{} shell. No tilde
Fred Drake45c9df61997-12-29 15:55:10 +00007expansion is done, but \code{*}, \code{?}, and character ranges
8expressed with \code{[]} will be correctly matched. This is done by
Fred Drakead511921998-02-16 21:25:53 +00009using the \function{os.listdir()} and \function{fnmatch.fnmatch()}
10functions in concert, and not by actually invoking a subshell. (For
11tilde and shell variable expansion, use \function{os.path.expanduser()}
12and \function{os.path.expandvars()}.)
Guido van Rossume6d579d1997-03-25 22:07:53 +000013
14\begin{funcdesc}{glob}{pathname}
15Returns a possibly-empty list of path names that match \var{pathname},
16which must be a string containing a path specification.
17\var{pathname} can be either absolute (like
Fred Drake2de75ec1998-04-09 14:12:11 +000018\file{/usr/src/Python-1.5/Makefile}) or relative (like
Guido van Rossume6d579d1997-03-25 22:07:53 +000019\file{../../Tools/*.gif}), and can contain shell-style wildcards.
20\end{funcdesc}
21
22For example, consider a directory containing only the following files:
Fred Drake5bfe4851998-04-03 06:14:54 +000023\file{1.gif}, \file{2.txt}, and \file{card.gif}. \function{glob()}
Guido van Rossume6d579d1997-03-25 22:07:53 +000024will produce the following results. Notice how any leading components
25of the path are preserved.
26
Fred Drake19479911998-02-13 06:58:54 +000027\begin{verbatim}
Guido van Rossume6d579d1997-03-25 22:07:53 +000028>>> import glob
29>>> glob.glob('./[0-9].*')
30['./1.gif', './2.txt']
31>>> glob.glob('*.gif')
32['1.gif', 'card.gif']
33>>> glob.glob('?.gif')
34['1.gif']
Fred Drake19479911998-02-13 06:58:54 +000035\end{verbatim}