Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{fnmatch} --- |
Fred Drake | 06a73f0 | 1999-06-10 21:23:31 +0000 | [diff] [blame] | 2 | \UNIX{} filename pattern matching} |
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}{fnmatch} |
Fred Drake | 17a2b64 | 2000-10-09 18:12:29 +0000 | [diff] [blame] | 5 | \modulesynopsis{\UNIX\ shell style filename pattern matching.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | e76b7a8 | 1997-04-27 21:25:52 +0000 | [diff] [blame] | 7 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 8 | \index{filenames!wildcard expansion} |
| 9 | |
Fred Drake | 48022db | 1998-01-11 19:06:37 +0000 | [diff] [blame] | 10 | This module provides support for \UNIX{} shell-style wildcards, which |
| 11 | are \emph{not} the same as regular expressions (which are documented |
Fred Drake | 8d2c0c2 | 1999-03-16 16:40:01 +0000 | [diff] [blame] | 12 | in the \refmodule{re}\refstmodindex{re} module). The special |
| 13 | characters used in shell-style wildcards are: |
Fred Drake | 009ab92 | 1998-02-16 21:37:58 +0000 | [diff] [blame] | 14 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 15 | \begin{tableii}{c|l}{code}{Pattern}{Meaning} |
| 16 | \lineii{*}{matches everything} |
| 17 | \lineii{?}{matches any single character} |
| 18 | \lineii{[\var{seq}]}{matches any character in \var{seq}} |
| 19 | \lineii{[!\var{seq}]}{matches any character not in \var{seq}} |
| 20 | \end{tableii} |
Guido van Rossum | e76b7a8 | 1997-04-27 21:25:52 +0000 | [diff] [blame] | 21 | |
Fred Drake | 48022db | 1998-01-11 19:06:37 +0000 | [diff] [blame] | 22 | Note that the filename separator (\code{'/'} on \UNIX{}) is \emph{not} |
Fred Drake | 06a73f0 | 1999-06-10 21:23:31 +0000 | [diff] [blame] | 23 | special to this module. See module |
| 24 | \refmodule{glob}\refstmodindex{glob} for pathname expansion |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 25 | (\refmodule{glob} uses \function{fnmatch()} to match pathname |
| 26 | segments). Similarly, filenames starting with a period are |
| 27 | not special for this module, and are matched by the \code{*} and |
| 28 | \code{?} patterns. |
Guido van Rossum | e76b7a8 | 1997-04-27 21:25:52 +0000 | [diff] [blame] | 29 | |
Fred Drake | 798654f | 1997-11-30 05:53:22 +0000 | [diff] [blame] | 30 | |
Fred Drake | 48022db | 1998-01-11 19:06:37 +0000 | [diff] [blame] | 31 | \begin{funcdesc}{fnmatch}{filename, pattern} |
Guido van Rossum | e76b7a8 | 1997-04-27 21:25:52 +0000 | [diff] [blame] | 32 | Test whether the \var{filename} string matches the \var{pattern} |
| 33 | string, returning true or false. If the operating system is |
| 34 | case-insensitive, then both parameters will be normalized to all |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 35 | lower- or upper-case before the comparison is performed. If you |
| 36 | require a case-sensitive comparison regardless of whether that's |
Fred Drake | 009ab92 | 1998-02-16 21:37:58 +0000 | [diff] [blame] | 37 | standard for your operating system, use \function{fnmatchcase()} |
| 38 | instead. |
Guido van Rossum | e76b7a8 | 1997-04-27 21:25:52 +0000 | [diff] [blame] | 39 | \end{funcdesc} |
| 40 | |
Fred Drake | 48022db | 1998-01-11 19:06:37 +0000 | [diff] [blame] | 41 | \begin{funcdesc}{fnmatchcase}{filename, pattern} |
Guido van Rossum | e76b7a8 | 1997-04-27 21:25:52 +0000 | [diff] [blame] | 42 | Test whether \var{filename} matches \var{pattern}, returning true or |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 43 | false; the comparison is case-sensitive. |
Guido van Rossum | e76b7a8 | 1997-04-27 21:25:52 +0000 | [diff] [blame] | 44 | \end{funcdesc} |
| 45 | |
Martin v. Löwis | e2ccb89 | 2001-06-07 19:01:24 +0000 | [diff] [blame] | 46 | \begin{funcdesc}{filter}{names, pattern} |
| 47 | Return the subset of the list of \var{names} that match \var{pattern}. |
| 48 | It is the same as \code{[n for n in names if fnmatch(n, pattern)]}, but |
| 49 | implemented more efficiently. |
Martin v. Löwis | 918f2c7 | 2001-06-16 08:14:04 +0000 | [diff] [blame] | 50 | \versionadded{2.2} |
Martin v. Löwis | e2ccb89 | 2001-06-07 19:01:24 +0000 | [diff] [blame] | 51 | \end{funcdesc} |
Fred Drake | 8d2c0c2 | 1999-03-16 16:40:01 +0000 | [diff] [blame] | 52 | |
Fred Drake | 48022db | 1998-01-11 19:06:37 +0000 | [diff] [blame] | 53 | \begin{seealso} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 54 | \seemodule{glob}{\UNIX{} shell-style path expansion.} |
Fred Drake | 48022db | 1998-01-11 19:06:37 +0000 | [diff] [blame] | 55 | \end{seealso} |