Guido van Rossum | e6d579d | 1997-03-25 22:07:53 +0000 | [diff] [blame] | 1 | \section{Standard Module \sectcode{glob}} |
| 2 | \stmodindex{glob} |
| 3 | \renewcommand{\indexsubitem}{(in module glob)} |
| 4 | |
| 5 | The \code{glob} module finds all the pathnames matching a specified |
| 6 | pattern according to the rules used by the \UNIX{} shell. No tilde |
| 7 | expansion is done, but \verb\*\, \verb\?\, and character ranges |
| 8 | expressed with \verb\[]\ will be correctly matched. This is done by |
| 9 | using the \code{os.listdir()} and \code{fnmatch.fnmatch()} functions |
| 10 | in concert, and not by actually invoking a subshell. (For tilde and |
| 11 | shell variable expansion, use \code{os.path.expanduser(}) and |
| 12 | \code{os.path.expandvars()}.) |
| 13 | |
| 14 | \begin{funcdesc}{glob}{pathname} |
| 15 | Returns a possibly-empty list of path names that match \var{pathname}, |
| 16 | which must be a string containing a path specification. |
| 17 | \var{pathname} can be either absolute (like |
| 18 | \file{/usr/src/Python1.4/Makefile}) or relative (like |
| 19 | \file{../../Tools/*.gif}), and can contain shell-style wildcards. |
| 20 | \end{funcdesc} |
| 21 | |
| 22 | For example, consider a directory containing only the following files: |
| 23 | \file{1.gif}, \file{2.txt}, and \file{card.gif}. \code{glob.glob()} |
| 24 | will produce the following results. Notice how any leading components |
| 25 | of the path are preserved. |
| 26 | |
| 27 | \begin{verbatim} |
| 28 | >>> 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'] |
| 35 | \end{verbatim} |