blob: 9fc9c7c4d518936ad28e999f86a0987163c2310a [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
19+------------+------------------------------------+
20| Pattern | Meaning |
21+============+====================================+
22| ``*`` | matches everything |
23+------------+------------------------------------+
24| ``?`` | matches any single character |
25+------------+------------------------------------+
26| ``[seq]`` | matches any character in *seq* |
27+------------+------------------------------------+
28| ``[!seq]`` | matches any character not in *seq* |
29+------------+------------------------------------+
30
Ezio Melottia39a22d2012-11-17 17:38:11 +020031For a literal match, wrap the meta-characters in brackets.
32For example, ``'[?]'`` matches the character ``'?'``.
33
Georg Brandl116aa622007-08-15 14:28:22 +000034.. index:: module: glob
35
36Note that the filename separator (``'/'`` on Unix) is *not* special to this
37module. See module :mod:`glob` for pathname expansion (:mod:`glob` uses
38:func:`fnmatch` to match pathname segments). Similarly, filenames starting with
39a period are not special for this module, and are matched by the ``*`` and ``?``
40patterns.
41
42
43.. function:: fnmatch(filename, pattern)
44
Ezio Melottiec5c8b82009-09-23 21:42:25 +000045 Test whether the *filename* string matches the *pattern* string, returning
46 :const:`True` or :const:`False`. If the operating system is case-insensitive,
47 then both parameters will be normalized to all lower- or upper-case before
48 the comparison is performed. :func:`fnmatchcase` can be used to perform a
49 case-sensitive comparison, regardless of whether that's standard for the
50 operating system.
Georg Brandl116aa622007-08-15 14:28:22 +000051
52 This example will print all file names in the current directory with the
53 extension ``.txt``::
54
55 import fnmatch
56 import os
57
58 for file in os.listdir('.'):
59 if fnmatch.fnmatch(file, '*.txt'):
Georg Brandl6911e3c2007-09-04 07:15:32 +000060 print(file)
Georg Brandl116aa622007-08-15 14:28:22 +000061
62
63.. function:: fnmatchcase(filename, pattern)
64
Ezio Melottiec5c8b82009-09-23 21:42:25 +000065 Test whether *filename* matches *pattern*, returning :const:`True` or
66 :const:`False`; the comparison is case-sensitive.
Georg Brandl116aa622007-08-15 14:28:22 +000067
68
69.. function:: filter(names, pattern)
70
71 Return the subset of the list of *names* that match *pattern*. It is the same as
72 ``[n for n in names if fnmatch(n, pattern)]``, but implemented more efficiently.
73
Georg Brandl116aa622007-08-15 14:28:22 +000074
75.. function:: translate(pattern)
76
Serhiy Storchakaa65a4742016-10-27 22:47:08 +030077 Return the shell-style *pattern* converted to a regular expression for
78 using with :func:`re.match`.
Georg Brandl116aa622007-08-15 14:28:22 +000079
Christian Heimesfe337bf2008-03-23 21:54:12 +000080 Example:
Georg Brandl116aa622007-08-15 14:28:22 +000081
82 >>> import fnmatch, re
83 >>>
84 >>> regex = fnmatch.translate('*.txt')
85 >>> regex
R David Murray4590c3d2015-05-02 15:08:22 -040086 '.*\\.txt\\Z(?ms)'
Georg Brandl116aa622007-08-15 14:28:22 +000087 >>> reobj = re.compile(regex)
Ezio Melottiec5c8b82009-09-23 21:42:25 +000088 >>> reobj.match('foobar.txt')
Ezio Melotti75719412013-11-23 20:27:27 +020089 <_sre.SRE_Match object; span=(0, 10), match='foobar.txt'>
Georg Brandl116aa622007-08-15 14:28:22 +000090
91
Georg Brandl116aa622007-08-15 14:28:22 +000092.. seealso::
93
94 Module :mod:`glob`
95 Unix shell-style path expansion.