blob: 7561880b76f4071724ab2f03d4fc6da3f4fb402d [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
12This module provides support for Unix shell-style wildcards, which are *not* the
13same as regular expressions (which are documented in the :mod:`re` module). The
14special characters used in shell-style wildcards are:
15
16+------------+------------------------------------+
17| Pattern | Meaning |
18+============+====================================+
19| ``*`` | matches everything |
20+------------+------------------------------------+
21| ``?`` | matches any single character |
22+------------+------------------------------------+
23| ``[seq]`` | matches any character in *seq* |
24+------------+------------------------------------+
25| ``[!seq]`` | matches any character not in *seq* |
26+------------+------------------------------------+
27
28.. index:: module: glob
29
30Note that the filename separator (``'/'`` on Unix) is *not* special to this
31module. See module :mod:`glob` for pathname expansion (:mod:`glob` uses
32:func:`fnmatch` to match pathname segments). Similarly, filenames starting with
33a period are not special for this module, and are matched by the ``*`` and ``?``
34patterns.
35
36
37.. function:: fnmatch(filename, pattern)
38
39 Test whether the *filename* string matches the *pattern* string, returning true
40 or false. If the operating system is case-insensitive, then both parameters
41 will be normalized to all lower- or upper-case before the comparison is
42 performed. If you require a case-sensitive comparison regardless of whether
43 that's standard for your operating system, use :func:`fnmatchcase` instead.
44
45 This example will print all file names in the current directory with the
46 extension ``.txt``::
47
48 import fnmatch
49 import os
50
51 for file in os.listdir('.'):
52 if fnmatch.fnmatch(file, '*.txt'):
Georg Brandl6911e3c2007-09-04 07:15:32 +000053 print(file)
Georg Brandl116aa622007-08-15 14:28:22 +000054
55
56.. function:: fnmatchcase(filename, pattern)
57
58 Test whether *filename* matches *pattern*, returning true or false; the
59 comparison is case-sensitive.
60
61
62.. function:: filter(names, pattern)
63
64 Return the subset of the list of *names* that match *pattern*. It is the same as
65 ``[n for n in names if fnmatch(n, pattern)]``, but implemented more efficiently.
66
Georg Brandl116aa622007-08-15 14:28:22 +000067
68.. function:: translate(pattern)
69
70 Return the shell-style *pattern* converted to a regular expression.
71
Christian Heimesfe337bf2008-03-23 21:54:12 +000072 Example:
Georg Brandl116aa622007-08-15 14:28:22 +000073
74 >>> import fnmatch, re
75 >>>
76 >>> regex = fnmatch.translate('*.txt')
77 >>> regex
78 '.*\\.txt$'
79 >>> reobj = re.compile(regex)
Georg Brandl6911e3c2007-09-04 07:15:32 +000080 >>> print(reobj.match('foobar.txt'))
Georg Brandl116aa622007-08-15 14:28:22 +000081 <_sre.SRE_Match object at 0x...>
82
83
84.. seealso::
85
86 Module :mod:`glob`
87 Unix shell-style path expansion.
88