Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{glob} --- |
| 2 | \UNIX{} shell style pathname pattern expansion.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | \declaremodule{standard}{glob} |
| 4 | |
| 5 | \modulesynopsis{\UNIX{} shell style pathname pattern expansion.} |
| 6 | |
Guido van Rossum | e6d579d | 1997-03-25 22:07:53 +0000 | [diff] [blame] | 7 | |
Fred Drake | ad51192 | 1998-02-16 21:25:53 +0000 | [diff] [blame] | 8 | The \module{glob} module finds all the pathnames matching a specified |
Guido van Rossum | e6d579d | 1997-03-25 22:07:53 +0000 | [diff] [blame] | 9 | pattern according to the rules used by the \UNIX{} shell. No tilde |
Fred Drake | 45c9df6 | 1997-12-29 15:55:10 +0000 | [diff] [blame] | 10 | expansion is done, but \code{*}, \code{?}, and character ranges |
| 11 | expressed with \code{[]} will be correctly matched. This is done by |
Fred Drake | ad51192 | 1998-02-16 21:25:53 +0000 | [diff] [blame] | 12 | using the \function{os.listdir()} and \function{fnmatch.fnmatch()} |
| 13 | functions in concert, and not by actually invoking a subshell. (For |
| 14 | tilde and shell variable expansion, use \function{os.path.expanduser()} |
| 15 | and \function{os.path.expandvars()}.) |
Guido van Rossum | e6d579d | 1997-03-25 22:07:53 +0000 | [diff] [blame] | 16 | |
| 17 | \begin{funcdesc}{glob}{pathname} |
| 18 | Returns a possibly-empty list of path names that match \var{pathname}, |
| 19 | which must be a string containing a path specification. |
| 20 | \var{pathname} can be either absolute (like |
Fred Drake | 2de75ec | 1998-04-09 14:12:11 +0000 | [diff] [blame] | 21 | \file{/usr/src/Python-1.5/Makefile}) or relative (like |
Guido van Rossum | e6d579d | 1997-03-25 22:07:53 +0000 | [diff] [blame] | 22 | \file{../../Tools/*.gif}), and can contain shell-style wildcards. |
| 23 | \end{funcdesc} |
| 24 | |
| 25 | For example, consider a directory containing only the following files: |
Fred Drake | 5bfe485 | 1998-04-03 06:14:54 +0000 | [diff] [blame] | 26 | \file{1.gif}, \file{2.txt}, and \file{card.gif}. \function{glob()} |
Guido van Rossum | e6d579d | 1997-03-25 22:07:53 +0000 | [diff] [blame] | 27 | will produce the following results. Notice how any leading components |
| 28 | of the path are preserved. |
| 29 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 30 | \begin{verbatim} |
Guido van Rossum | e6d579d | 1997-03-25 22:07:53 +0000 | [diff] [blame] | 31 | >>> 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 Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 38 | \end{verbatim} |