blob: cb1fa106f946ec0438c8c538f70324c04518b3d1 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`fnmatch` --- Unix filename pattern matching
3=================================================
4
5.. module:: fnmatch
6 :synopsis: Unix shell style filename pattern matching.
7
8
9.. index:: single: filenames; wildcard expansion
10
11.. index:: module: re
12
13This module provides support for Unix shell-style wildcards, which are *not* the
14same as regular expressions (which are documented in the :mod:`re` module). The
15special characters used in shell-style wildcards are:
16
17+------------+------------------------------------+
18| Pattern | Meaning |
19+============+====================================+
20| ``*`` | matches everything |
21+------------+------------------------------------+
22| ``?`` | matches any single character |
23+------------+------------------------------------+
24| ``[seq]`` | matches any character in *seq* |
25+------------+------------------------------------+
26| ``[!seq]`` | matches any character not in *seq* |
27+------------+------------------------------------+
28
29.. index:: module: glob
30
31Note that the filename separator (``'/'`` on Unix) is *not* special to this
32module. See module :mod:`glob` for pathname expansion (:mod:`glob` uses
33:func:`fnmatch` to match pathname segments). Similarly, filenames starting with
34a period are not special for this module, and are matched by the ``*`` and ``?``
35patterns.
36
Raymond Hettingere0e08222010-11-06 07:10:31 +000037.. seealso::
38
39 Latest version of the `fnmatch Python source code
40 <http://svn.python.org/view/python/branches/release27-maint/Lib/fnmatch.py?view=markup>`_
Georg Brandl8ec7f652007-08-15 14:28:01 +000041
42.. function:: fnmatch(filename, pattern)
43
Ezio Melotti2fd35922009-09-23 21:36:39 +000044 Test whether the *filename* string matches the *pattern* string, returning
45 :const:`True` or :const:`False`. If the operating system is case-insensitive,
46 then both parameters will be normalized to all lower- or upper-case before
47 the comparison is performed. :func:`fnmatchcase` can be used to perform a
48 case-sensitive comparison, regardless of whether that's standard for the
49 operating system.
Georg Brandl8ec7f652007-08-15 14:28:01 +000050
51 This example will print all file names in the current directory with the
52 extension ``.txt``::
53
54 import fnmatch
55 import os
56
57 for file in os.listdir('.'):
58 if fnmatch.fnmatch(file, '*.txt'):
59 print file
60
61
62.. function:: fnmatchcase(filename, pattern)
63
Ezio Melotti2fd35922009-09-23 21:36:39 +000064 Test whether *filename* matches *pattern*, returning :const:`True` or
65 :const:`False`; the comparison is case-sensitive.
Georg Brandl8ec7f652007-08-15 14:28:01 +000066
67
68.. function:: filter(names, pattern)
69
70 Return the subset of the list of *names* that match *pattern*. It is the same as
71 ``[n for n in names if fnmatch(n, pattern)]``, but implemented more efficiently.
72
73 .. versionadded:: 2.2
74
75
76.. function:: translate(pattern)
77
78 Return the shell-style *pattern* converted to a regular expression.
79
Georg Brandla04b03b2010-10-06 09:47:17 +000080 Be aware there is no way to quote meta-characters.
81
Georg Brandle8f1b002008-03-22 22:04:10 +000082 Example:
Georg Brandl8ec7f652007-08-15 14:28:01 +000083
84 >>> import fnmatch, re
85 >>>
86 >>> regex = fnmatch.translate('*.txt')
87 >>> regex
88 '.*\\.txt$'
89 >>> reobj = re.compile(regex)
Ezio Melotti2fd35922009-09-23 21:36:39 +000090 >>> reobj.match('foobar.txt')
Georg Brandl8ec7f652007-08-15 14:28:01 +000091 <_sre.SRE_Match object at 0x...>
92
93
94.. seealso::
95
96 Module :mod:`glob`
97 Unix shell-style path expansion.
98