blob: 49119802a65a421e3917d86fc7812e368729f6e1 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +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
Éric Araujo29a0b572011-08-19 02:14:03 +020012**Source code:** :source:`Lib/fnmatch.py`
13
14--------------
15
Georg Brandl8ec7f652007-08-15 14:28:01 +000016This module provides support for Unix shell-style wildcards, which are *not* the
17same as regular expressions (which are documented in the :mod:`re` module). The
18special characters used in shell-style wildcards are:
19
20+------------+------------------------------------+
21| Pattern | Meaning |
22+============+====================================+
23| ``*`` | matches everything |
24+------------+------------------------------------+
25| ``?`` | matches any single character |
26+------------+------------------------------------+
27| ``[seq]`` | matches any character in *seq* |
28+------------+------------------------------------+
29| ``[!seq]`` | matches any character not in *seq* |
30+------------+------------------------------------+
31
32.. index:: module: glob
33
34Note that the filename separator (``'/'`` on Unix) is *not* special to this
35module. See module :mod:`glob` for pathname expansion (:mod:`glob` uses
36:func:`fnmatch` to match pathname segments). Similarly, filenames starting with
37a period are not special for this module, and are matched by the ``*`` and ``?``
38patterns.
39
40
41.. function:: fnmatch(filename, pattern)
42
Ezio Melotti2fd35922009-09-23 21:36:39 +000043 Test whether the *filename* string matches the *pattern* string, returning
44 :const:`True` or :const:`False`. If the operating system is case-insensitive,
45 then both parameters will be normalized to all lower- or upper-case before
46 the comparison is performed. :func:`fnmatchcase` can be used to perform a
47 case-sensitive comparison, regardless of whether that's standard for the
48 operating system.
Georg Brandl8ec7f652007-08-15 14:28:01 +000049
50 This example will print all file names in the current directory with the
51 extension ``.txt``::
52
53 import fnmatch
54 import os
55
56 for file in os.listdir('.'):
57 if fnmatch.fnmatch(file, '*.txt'):
58 print file
59
60
61.. function:: fnmatchcase(filename, pattern)
62
Ezio Melotti2fd35922009-09-23 21:36:39 +000063 Test whether *filename* matches *pattern*, returning :const:`True` or
64 :const:`False`; the comparison is case-sensitive.
Georg Brandl8ec7f652007-08-15 14:28:01 +000065
66
67.. function:: filter(names, pattern)
68
69 Return the subset of the list of *names* that match *pattern*. It is the same as
70 ``[n for n in names if fnmatch(n, pattern)]``, but implemented more efficiently.
71
72 .. versionadded:: 2.2
73
74
75.. function:: translate(pattern)
76
77 Return the shell-style *pattern* converted to a regular expression.
78
Georg Brandla04b03b2010-10-06 09:47:17 +000079 Be aware there is no way to quote meta-characters.
80
Georg Brandle8f1b002008-03-22 22:04:10 +000081 Example:
Georg Brandl8ec7f652007-08-15 14:28:01 +000082
83 >>> import fnmatch, re
84 >>>
85 >>> regex = fnmatch.translate('*.txt')
86 >>> regex
87 '.*\\.txt$'
88 >>> reobj = re.compile(regex)
Ezio Melotti2fd35922009-09-23 21:36:39 +000089 >>> reobj.match('foobar.txt')
Georg Brandl8ec7f652007-08-15 14:28:01 +000090 <_sre.SRE_Match object at 0x...>
91
92
93.. seealso::
94
95 Module :mod:`glob`
96 Unix shell-style path expansion.