blob: 3b9a6c3aeb8583cfc47b613ed68452a8100d002c [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`fnmatch` --- Unix filename pattern matching
2=================================================
3
4.. module:: fnmatch
5 :synopsis: Unix shell style filename pattern matching.
6
7
8.. index:: single: filenames; wildcard expansion
9
10.. index:: module: re
11
Raymond Hettinger10480942011-01-10 03:26:08 +000012**Source code:** :source:`Lib/fnmatch.py`
13
Georg Brandl116aa622007-08-15 14:28:22 +000014This module provides support for Unix shell-style wildcards, which are *not* the
15same as regular expressions (which are documented in the :mod:`re` module). The
16special characters used in shell-style wildcards are:
17
18+------------+------------------------------------+
19| Pattern | Meaning |
20+============+====================================+
21| ``*`` | matches everything |
22+------------+------------------------------------+
23| ``?`` | matches any single character |
24+------------+------------------------------------+
25| ``[seq]`` | matches any character in *seq* |
26+------------+------------------------------------+
27| ``[!seq]`` | matches any character not in *seq* |
28+------------+------------------------------------+
29
30.. index:: module: glob
31
32Note that the filename separator (``'/'`` on Unix) is *not* special to this
33module. See module :mod:`glob` for pathname expansion (:mod:`glob` uses
34:func:`fnmatch` to match pathname segments). Similarly, filenames starting with
35a period are not special for this module, and are matched by the ``*`` and ``?``
36patterns.
37
38
39.. function:: fnmatch(filename, pattern)
40
Ezio Melottiec5c8b82009-09-23 21:42:25 +000041 Test whether the *filename* string matches the *pattern* string, returning
42 :const:`True` or :const:`False`. If the operating system is case-insensitive,
43 then both parameters will be normalized to all lower- or upper-case before
44 the comparison is performed. :func:`fnmatchcase` can be used to perform a
45 case-sensitive comparison, regardless of whether that's standard for the
46 operating system.
Georg Brandl116aa622007-08-15 14:28:22 +000047
48 This example will print all file names in the current directory with the
49 extension ``.txt``::
50
51 import fnmatch
52 import os
53
54 for file in os.listdir('.'):
55 if fnmatch.fnmatch(file, '*.txt'):
Georg Brandl6911e3c2007-09-04 07:15:32 +000056 print(file)
Georg Brandl116aa622007-08-15 14:28:22 +000057
58
59.. function:: fnmatchcase(filename, pattern)
60
Ezio Melottiec5c8b82009-09-23 21:42:25 +000061 Test whether *filename* matches *pattern*, returning :const:`True` or
62 :const:`False`; the comparison is case-sensitive.
Georg Brandl116aa622007-08-15 14:28:22 +000063
64
65.. function:: filter(names, pattern)
66
67 Return the subset of the list of *names* that match *pattern*. It is the same as
68 ``[n for n in names if fnmatch(n, pattern)]``, but implemented more efficiently.
69
Georg Brandl116aa622007-08-15 14:28:22 +000070
71.. function:: translate(pattern)
72
73 Return the shell-style *pattern* converted to a regular expression.
74
Brett Cannon030244a2010-07-23 16:58:21 +000075 Be aware there is no way to quote meta-characters.
76
Christian Heimesfe337bf2008-03-23 21:54:12 +000077 Example:
Georg Brandl116aa622007-08-15 14:28:22 +000078
79 >>> import fnmatch, re
80 >>>
81 >>> regex = fnmatch.translate('*.txt')
82 >>> regex
83 '.*\\.txt$'
84 >>> reobj = re.compile(regex)
Ezio Melottiec5c8b82009-09-23 21:42:25 +000085 >>> reobj.match('foobar.txt')
Georg Brandl116aa622007-08-15 14:28:22 +000086 <_sre.SRE_Match object at 0x...>
87
88
Georg Brandl116aa622007-08-15 14:28:22 +000089.. seealso::
90
91 Module :mod:`glob`
92 Unix shell-style path expansion.