blob: fc4b97a3126ab9488c27b819f9f12b4875094b45 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{fnmatch} ---
Fred Drake06a73f01999-06-10 21:23:31 +00002 \UNIX{} filename pattern matching}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drake8d2c0c21999-03-16 16:40:01 +00004\declaremodule{standard}{fnmatch}
Fred Drake17a2b642000-10-09 18:12:29 +00005\modulesynopsis{\UNIX\ shell style filename pattern matching.}
Fred Drakeb91e9341998-07-23 17:59:49 +00006
Guido van Rossume76b7a81997-04-27 21:25:52 +00007
Fred Drake38e5d272000-04-03 20:13:55 +00008\index{filenames!wildcard expansion}
9
Fred Drake48022db1998-01-11 19:06:37 +000010This module provides support for \UNIX{} shell-style wildcards, which
11are \emph{not} the same as regular expressions (which are documented
Fred Drake8d2c0c21999-03-16 16:40:01 +000012in the \refmodule{re}\refstmodindex{re} module). The special
13characters used in shell-style wildcards are:
Fred Drake009ab921998-02-16 21:37:58 +000014
Fred Drake38e5d272000-04-03 20:13:55 +000015\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 Rossume76b7a81997-04-27 21:25:52 +000021
Fred Drakec37b65e2001-11-28 07:26:15 +000022Note that the filename separator (\code{'/'} on \UNIX) is \emph{not}
Fred Drake06a73f01999-06-10 21:23:31 +000023special to this module. See module
24\refmodule{glob}\refstmodindex{glob} for pathname expansion
Fred Drake38e5d272000-04-03 20:13:55 +000025(\refmodule{glob} uses \function{fnmatch()} to match pathname
26segments). Similarly, filenames starting with a period are
27not special for this module, and are matched by the \code{*} and
28\code{?} patterns.
Guido van Rossume76b7a81997-04-27 21:25:52 +000029
Fred Drake798654f1997-11-30 05:53:22 +000030
Fred Drake48022db1998-01-11 19:06:37 +000031\begin{funcdesc}{fnmatch}{filename, pattern}
Guido van Rossume76b7a81997-04-27 21:25:52 +000032Test whether the \var{filename} string matches the \var{pattern}
33string, returning true or false. If the operating system is
34case-insensitive, then both parameters will be normalized to all
Fred Drake38e5d272000-04-03 20:13:55 +000035lower- or upper-case before the comparison is performed. If you
36require a case-sensitive comparison regardless of whether that's
Fred Drake009ab921998-02-16 21:37:58 +000037standard for your operating system, use \function{fnmatchcase()}
38instead.
Guido van Rossume76b7a81997-04-27 21:25:52 +000039\end{funcdesc}
40
Fred Drake48022db1998-01-11 19:06:37 +000041\begin{funcdesc}{fnmatchcase}{filename, pattern}
Guido van Rossume76b7a81997-04-27 21:25:52 +000042Test whether \var{filename} matches \var{pattern}, returning true or
Fred Drake38e5d272000-04-03 20:13:55 +000043false; the comparison is case-sensitive.
Guido van Rossume76b7a81997-04-27 21:25:52 +000044\end{funcdesc}
45
Martin v. Löwise2ccb892001-06-07 19:01:24 +000046\begin{funcdesc}{filter}{names, pattern}
47Return the subset of the list of \var{names} that match \var{pattern}.
48It is the same as \code{[n for n in names if fnmatch(n, pattern)]}, but
49implemented more efficiently.
Martin v. Löwis918f2c72001-06-16 08:14:04 +000050\versionadded{2.2}
Martin v. Löwise2ccb892001-06-07 19:01:24 +000051\end{funcdesc}
Fred Drake8d2c0c21999-03-16 16:40:01 +000052
Fred Drake48022db1998-01-11 19:06:37 +000053\begin{seealso}
Fred Drake38e5d272000-04-03 20:13:55 +000054 \seemodule{glob}{\UNIX{} shell-style path expansion.}
Fred Drake48022db1998-01-11 19:06:37 +000055\end{seealso}