blob: ce07d326b395d80605e4b712343a6b79feb5a37e [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
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04007**Source code:** :source:`Lib/fnmatch.py`
Georg Brandl116aa622007-08-15 14:28:22 +00008
9.. index:: single: filenames; wildcard expansion
10
11.. index:: module: re
12
Raymond Hettinger4f707fd2011-01-10 19:54:11 +000013--------------
14
Georg Brandl116aa622007-08-15 14:28:22 +000015This module provides support for Unix shell-style wildcards, which are *not* the
16same as regular expressions (which are documented in the :mod:`re` module). The
17special characters used in shell-style wildcards are:
18
Serhiy Storchaka913876d2018-10-28 13:41:26 +020019.. index::
20 single: * (asterisk); in glob-style wildcards
21 single: ? (question mark); in glob-style wildcards
22 single: [] (square brackets); in glob-style wildcards
23 single: ! (exclamation); in glob-style wildcards
24 single: - (minus); in glob-style wildcards
25
Georg Brandl116aa622007-08-15 14:28:22 +000026+------------+------------------------------------+
27| Pattern | Meaning |
28+============+====================================+
29| ``*`` | matches everything |
30+------------+------------------------------------+
31| ``?`` | matches any single character |
32+------------+------------------------------------+
33| ``[seq]`` | matches any character in *seq* |
34+------------+------------------------------------+
35| ``[!seq]`` | matches any character not in *seq* |
36+------------+------------------------------------+
37
Ezio Melottia39a22d2012-11-17 17:38:11 +020038For a literal match, wrap the meta-characters in brackets.
39For example, ``'[?]'`` matches the character ``'?'``.
40
Georg Brandl116aa622007-08-15 14:28:22 +000041.. index:: module: glob
42
43Note that the filename separator (``'/'`` on Unix) is *not* special to this
44module. See module :mod:`glob` for pathname expansion (:mod:`glob` uses
Andrés Delfinoae31e3f2018-11-07 15:09:11 -030045:func:`.filter` to match pathname segments). Similarly, filenames starting with
Georg Brandl116aa622007-08-15 14:28:22 +000046a period are not special for this module, and are matched by the ``*`` and ``?``
47patterns.
48
49
50.. function:: fnmatch(filename, pattern)
51
Ezio Melottiec5c8b82009-09-23 21:42:25 +000052 Test whether the *filename* string matches the *pattern* string, returning
csabellae5f6e862017-06-10 00:42:11 -040053 :const:`True` or :const:`False`. Both parameters are case-normalized
54 using :func:`os.path.normcase`. :func:`fnmatchcase` can be used to perform a
Ezio Melottiec5c8b82009-09-23 21:42:25 +000055 case-sensitive comparison, regardless of whether that's standard for the
56 operating system.
Georg Brandl116aa622007-08-15 14:28:22 +000057
58 This example will print all file names in the current directory with the
59 extension ``.txt``::
60
61 import fnmatch
62 import os
63
64 for file in os.listdir('.'):
65 if fnmatch.fnmatch(file, '*.txt'):
Georg Brandl6911e3c2007-09-04 07:15:32 +000066 print(file)
Georg Brandl116aa622007-08-15 14:28:22 +000067
68
69.. function:: fnmatchcase(filename, pattern)
70
Ezio Melottiec5c8b82009-09-23 21:42:25 +000071 Test whether *filename* matches *pattern*, returning :const:`True` or
csabellae5f6e862017-06-10 00:42:11 -040072 :const:`False`; the comparison is case-sensitive and does not apply
73 :func:`os.path.normcase`.
Georg Brandl116aa622007-08-15 14:28:22 +000074
75
76.. function:: filter(names, pattern)
77
78 Return the subset of the list of *names* that match *pattern*. It is the same as
79 ``[n for n in names if fnmatch(n, pattern)]``, but implemented more efficiently.
80
Georg Brandl116aa622007-08-15 14:28:22 +000081
82.. function:: translate(pattern)
83
Serhiy Storchakaa65a4742016-10-27 22:47:08 +030084 Return the shell-style *pattern* converted to a regular expression for
85 using with :func:`re.match`.
Georg Brandl116aa622007-08-15 14:28:22 +000086
Christian Heimesfe337bf2008-03-23 21:54:12 +000087 Example:
Georg Brandl116aa622007-08-15 14:28:22 +000088
89 >>> import fnmatch, re
90 >>>
91 >>> regex = fnmatch.translate('*.txt')
92 >>> regex
Serhiy Storchakae0a220e2016-10-27 22:44:03 +030093 '(?s:.*\\.txt)\\Z'
Georg Brandl116aa622007-08-15 14:28:22 +000094 >>> reobj = re.compile(regex)
Ezio Melottiec5c8b82009-09-23 21:42:25 +000095 >>> reobj.match('foobar.txt')
Serhiy Storchaka0b5e61d2017-10-04 20:09:49 +030096 <re.Match object; span=(0, 10), match='foobar.txt'>
Georg Brandl116aa622007-08-15 14:28:22 +000097
98
Georg Brandl116aa622007-08-15 14:28:22 +000099.. seealso::
100
101 Module :mod:`glob`
102 Unix shell-style path expansion.