libfnmatch.tex 2.01 KB
Newer Older
Fred Drake's avatar
Fred Drake committed
1
\section{\module{fnmatch} ---
2
         \UNIX{} filename pattern matching}
3

4
\declaremodule{standard}{fnmatch}
5
\modulesynopsis{\UNIX\ shell style filename pattern matching.}
6

7

8 9
\index{filenames!wildcard expansion}

Fred Drake's avatar
Fred Drake committed
10 11
This module provides support for \UNIX{} shell-style wildcards, which
are \emph{not} the same as regular expressions (which are documented
12 13
in the \refmodule{re}\refstmodindex{re} module).  The special
characters used in shell-style wildcards are:
14

15 16 17 18 19 20
\begin{tableii}{c|l}{code}{Pattern}{Meaning}
  \lineii{*}{matches everything}
  \lineii{?}{matches any single character}
  \lineii{[\var{seq}]}{matches any character in \var{seq}}
  \lineii{[!\var{seq}]}{matches any character not in \var{seq}}
\end{tableii}
21

22
Note that the filename separator (\code{'/'} on \UNIX) is \emph{not}
23 24
special to this module.  See module
\refmodule{glob}\refstmodindex{glob} for pathname expansion
25 26 27 28
(\refmodule{glob} uses \function{fnmatch()} to match pathname
segments).  Similarly, filenames starting with a period are
not special for this module, and are matched by the \code{*} and
\code{?} patterns.
29

30

Fred Drake's avatar
Fred Drake committed
31
\begin{funcdesc}{fnmatch}{filename, pattern}
32 33 34
Test whether the \var{filename} string matches the \var{pattern}
string, returning true or false.  If the operating system is
case-insensitive, then both parameters will be normalized to all
35 36
lower- or upper-case before the comparison is performed.  If you
require a case-sensitive comparison regardless of whether that's
37 38
standard for your operating system, use \function{fnmatchcase()}
instead.
39 40
\end{funcdesc}

Fred Drake's avatar
Fred Drake committed
41
\begin{funcdesc}{fnmatchcase}{filename, pattern}
42
Test whether \var{filename} matches \var{pattern}, returning true or
43
false; the comparison is case-sensitive.
44 45
\end{funcdesc}

Martin v. Löwis's avatar
Martin v. Löwis committed
46 47 48 49
\begin{funcdesc}{filter}{names, pattern}
Return the subset of the list of \var{names} that match \var{pattern}.
It is the same as \code{[n for n in names if fnmatch(n, pattern)]}, but
implemented more efficiently.
50
\versionadded{2.2}
Martin v. Löwis's avatar
Martin v. Löwis committed
51
\end{funcdesc}
52

Fred Drake's avatar
Fred Drake committed
53
\begin{seealso}
54
  \seemodule{glob}{\UNIX{} shell-style path expansion.}
Fred Drake's avatar
Fred Drake committed
55
\end{seealso}