blob: 4ba6b77f8ed74b555474e1db25522e650814ab73 [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
Raymond Hettinger4f707fd2011-01-10 19:54:11 +000014--------------
15
Georg Brandl116aa622007-08-15 14:28:22 +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 Melottiec5c8b82009-09-23 21:42:25 +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 Brandl116aa622007-08-15 14:28:22 +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'):
Georg Brandl6911e3c2007-09-04 07:15:32 +000058 print(file)
Georg Brandl116aa622007-08-15 14:28:22 +000059
60
61.. function:: fnmatchcase(filename, pattern)
62
Ezio Melottiec5c8b82009-09-23 21:42:25 +000063 Test whether *filename* matches *pattern*, returning :const:`True` or
64 :const:`False`; the comparison is case-sensitive.
Georg Brandl116aa622007-08-15 14:28:22 +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
Georg Brandl116aa622007-08-15 14:28:22 +000072
73.. function:: translate(pattern)
74
75 Return the shell-style *pattern* converted to a regular expression.
76
Brett Cannon030244a2010-07-23 16:58:21 +000077 Be aware there is no way to quote meta-characters.
78
Christian Heimesfe337bf2008-03-23 21:54:12 +000079 Example:
Georg Brandl116aa622007-08-15 14:28:22 +000080
81 >>> import fnmatch, re
82 >>>
83 >>> regex = fnmatch.translate('*.txt')
84 >>> regex
85 '.*\\.txt$'
86 >>> reobj = re.compile(regex)
Ezio Melottiec5c8b82009-09-23 21:42:25 +000087 >>> reobj.match('foobar.txt')
Georg Brandl116aa622007-08-15 14:28:22 +000088 <_sre.SRE_Match object at 0x...>
89
90
Georg Brandl116aa622007-08-15 14:28:22 +000091.. seealso::
92
93 Module :mod:`glob`
94 Unix shell-style path expansion.