Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`fnmatch` --- Unix filename pattern matching |
| 2 | ================================================= |
| 3 | |
| 4 | .. module:: fnmatch |
| 5 | :synopsis: Unix shell style filename pattern matching. |
| 6 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 7 | **Source code:** :source:`Lib/fnmatch.py` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 8 | |
| 9 | .. index:: single: filenames; wildcard expansion |
| 10 | |
| 11 | .. index:: module: re |
| 12 | |
Raymond Hettinger | 4f707fd | 2011-01-10 19:54:11 +0000 | [diff] [blame] | 13 | -------------- |
| 14 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 15 | This module provides support for Unix shell-style wildcards, which are *not* the |
| 16 | same as regular expressions (which are documented in the :mod:`re` module). The |
| 17 | special characters used in shell-style wildcards are: |
| 18 | |
Serhiy Storchaka | 913876d | 2018-10-28 13:41:26 +0200 | [diff] [blame] | 19 | .. 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 26 | +------------+------------------------------------+ |
| 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 Melotti | a39a22d | 2012-11-17 17:38:11 +0200 | [diff] [blame] | 38 | For a literal match, wrap the meta-characters in brackets. |
| 39 | For example, ``'[?]'`` matches the character ``'?'``. |
| 40 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 41 | .. index:: module: glob |
| 42 | |
| 43 | Note that the filename separator (``'/'`` on Unix) is *not* special to this |
| 44 | module. See module :mod:`glob` for pathname expansion (:mod:`glob` uses |
Andrés Delfino | ae31e3f | 2018-11-07 15:09:11 -0300 | [diff] [blame] | 45 | :func:`.filter` to match pathname segments). Similarly, filenames starting with |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 46 | a period are not special for this module, and are matched by the ``*`` and ``?`` |
| 47 | patterns. |
| 48 | |
| 49 | |
| 50 | .. function:: fnmatch(filename, pattern) |
| 51 | |
Ezio Melotti | ec5c8b8 | 2009-09-23 21:42:25 +0000 | [diff] [blame] | 52 | Test whether the *filename* string matches the *pattern* string, returning |
csabella | e5f6e86 | 2017-06-10 00:42:11 -0400 | [diff] [blame] | 53 | :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 Melotti | ec5c8b8 | 2009-09-23 21:42:25 +0000 | [diff] [blame] | 55 | case-sensitive comparison, regardless of whether that's standard for the |
| 56 | operating system. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 57 | |
| 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 Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 66 | print(file) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 67 | |
| 68 | |
| 69 | .. function:: fnmatchcase(filename, pattern) |
| 70 | |
Ezio Melotti | ec5c8b8 | 2009-09-23 21:42:25 +0000 | [diff] [blame] | 71 | Test whether *filename* matches *pattern*, returning :const:`True` or |
csabella | e5f6e86 | 2017-06-10 00:42:11 -0400 | [diff] [blame] | 72 | :const:`False`; the comparison is case-sensitive and does not apply |
| 73 | :func:`os.path.normcase`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 74 | |
| 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 81 | |
| 82 | .. function:: translate(pattern) |
| 83 | |
Serhiy Storchaka | a65a474 | 2016-10-27 22:47:08 +0300 | [diff] [blame] | 84 | Return the shell-style *pattern* converted to a regular expression for |
| 85 | using with :func:`re.match`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 86 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 87 | Example: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 88 | |
| 89 | >>> import fnmatch, re |
| 90 | >>> |
| 91 | >>> regex = fnmatch.translate('*.txt') |
| 92 | >>> regex |
Serhiy Storchaka | e0a220e | 2016-10-27 22:44:03 +0300 | [diff] [blame] | 93 | '(?s:.*\\.txt)\\Z' |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 94 | >>> reobj = re.compile(regex) |
Ezio Melotti | ec5c8b8 | 2009-09-23 21:42:25 +0000 | [diff] [blame] | 95 | >>> reobj.match('foobar.txt') |
Serhiy Storchaka | 0b5e61d | 2017-10-04 20:09:49 +0300 | [diff] [blame] | 96 | <re.Match object; span=(0, 10), match='foobar.txt'> |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 97 | |
| 98 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 99 | .. seealso:: |
| 100 | |
| 101 | Module :mod:`glob` |
| 102 | Unix shell-style path expansion. |