Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{glob} --- |
Fred Drake | 8d2c0c2 | 1999-03-16 16:40:01 +0000 | [diff] [blame] | 2 | \UNIX{} style pathname pattern expansion} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | 8d2c0c2 | 1999-03-16 16:40:01 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{glob} |
Fred Drake | 17a2b64 | 2000-10-09 18:12:29 +0000 | [diff] [blame] | 5 | \modulesynopsis{\UNIX\ shell style pathname pattern expansion.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 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()}.) |
Fred Drake | 8d2c0c2 | 1999-03-16 16:40:01 +0000 | [diff] [blame] | 16 | \index{filenames!pathname expansion} |
Guido van Rossum | e6d579d | 1997-03-25 22:07:53 +0000 | [diff] [blame] | 17 | |
| 18 | \begin{funcdesc}{glob}{pathname} |
| 19 | Returns a possibly-empty list of path names that match \var{pathname}, |
| 20 | which must be a string containing a path specification. |
| 21 | \var{pathname} can be either absolute (like |
Fred Drake | 2de75ec | 1998-04-09 14:12:11 +0000 | [diff] [blame] | 22 | \file{/usr/src/Python-1.5/Makefile}) or relative (like |
Fred Drake | 8d2c0c2 | 1999-03-16 16:40:01 +0000 | [diff] [blame] | 23 | \file{../../Tools/*/*.gif}), and can contain shell-style wildcards. |
Guido van Rossum | e6d579d | 1997-03-25 22:07:53 +0000 | [diff] [blame] | 24 | \end{funcdesc} |
| 25 | |
| 26 | For example, consider a directory containing only the following files: |
Fred Drake | 5bfe485 | 1998-04-03 06:14:54 +0000 | [diff] [blame] | 27 | \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] | 28 | will produce the following results. Notice how any leading components |
| 29 | of the path are preserved. |
| 30 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 31 | \begin{verbatim} |
Guido van Rossum | e6d579d | 1997-03-25 22:07:53 +0000 | [diff] [blame] | 32 | >>> 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 Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 39 | \end{verbatim} |
Fred Drake | 8d2c0c2 | 1999-03-16 16:40:01 +0000 | [diff] [blame] | 40 | |
| 41 | |
| 42 | \begin{seealso} |
| 43 | \seemodule{fnmatch}{Shell-style filename (not path) expansion} |
| 44 | \end{seealso} |