/Doc/library/fnmatch.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 92 lines · 61 code · 31 blank · 0 comment · 0 complexity · 82116729dff609ea2747465a8dd0815b MD5 · raw file

  1. :mod:`fnmatch` --- Unix filename pattern matching
  2. =================================================
  3. .. module:: fnmatch
  4. :synopsis: Unix shell style filename pattern matching.
  5. .. index:: single: filenames; wildcard expansion
  6. .. index:: module: re
  7. This module provides support for Unix shell-style wildcards, which are *not* the
  8. same as regular expressions (which are documented in the :mod:`re` module). The
  9. special characters used in shell-style wildcards are:
  10. +------------+------------------------------------+
  11. | Pattern | Meaning |
  12. +============+====================================+
  13. | ``*`` | matches everything |
  14. +------------+------------------------------------+
  15. | ``?`` | matches any single character |
  16. +------------+------------------------------------+
  17. | ``[seq]`` | matches any character in *seq* |
  18. +------------+------------------------------------+
  19. | ``[!seq]`` | matches any character not in *seq* |
  20. +------------+------------------------------------+
  21. .. index:: module: glob
  22. Note that the filename separator (``'/'`` on Unix) is *not* special to this
  23. module. See module :mod:`glob` for pathname expansion (:mod:`glob` uses
  24. :func:`fnmatch` to match pathname segments). Similarly, filenames starting with
  25. a period are not special for this module, and are matched by the ``*`` and ``?``
  26. patterns.
  27. .. function:: fnmatch(filename, pattern)
  28. Test whether the *filename* string matches the *pattern* string, returning
  29. :const:`True` or :const:`False`. If the operating system is case-insensitive,
  30. then both parameters will be normalized to all lower- or upper-case before
  31. the comparison is performed. :func:`fnmatchcase` can be used to perform a
  32. case-sensitive comparison, regardless of whether that's standard for the
  33. operating system.
  34. This example will print all file names in the current directory with the
  35. extension ``.txt``::
  36. import fnmatch
  37. import os
  38. for file in os.listdir('.'):
  39. if fnmatch.fnmatch(file, '*.txt'):
  40. print file
  41. .. function:: fnmatchcase(filename, pattern)
  42. Test whether *filename* matches *pattern*, returning :const:`True` or
  43. :const:`False`; the comparison is case-sensitive.
  44. .. function:: filter(names, pattern)
  45. Return the subset of the list of *names* that match *pattern*. It is the same as
  46. ``[n for n in names if fnmatch(n, pattern)]``, but implemented more efficiently.
  47. .. versionadded:: 2.2
  48. .. function:: translate(pattern)
  49. Return the shell-style *pattern* converted to a regular expression.
  50. Example:
  51. >>> import fnmatch, re
  52. >>>
  53. >>> regex = fnmatch.translate('*.txt')
  54. >>> regex
  55. '.*\\.txt$'
  56. >>> reobj = re.compile(regex)
  57. >>> reobj.match('foobar.txt')
  58. <_sre.SRE_Match object at 0x...>
  59. .. seealso::
  60. Module :mod:`glob`
  61. Unix shell-style path expansion.