/Doc/library/filecmp.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 163 lines · 87 code · 76 blank · 0 comment · 0 complexity · 5384b22487605e6ab649930003f8b874 MD5 · raw file

  1. :mod:`filecmp` --- File and Directory Comparisons
  2. =================================================
  3. .. module:: filecmp
  4. :synopsis: Compare files efficiently.
  5. .. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
  6. The :mod:`filecmp` module defines functions to compare files and directories,
  7. with various optional time/correctness trade-offs. For comparing files,
  8. see also the :mod:`difflib` module.
  9. The :mod:`filecmp` module defines the following functions:
  10. .. function:: cmp(f1, f2[, shallow])
  11. Compare the files named *f1* and *f2*, returning ``True`` if they seem equal,
  12. ``False`` otherwise.
  13. Unless *shallow* is given and is false, files with identical :func:`os.stat`
  14. signatures are taken to be equal.
  15. Files that were compared using this function will not be compared again unless
  16. their :func:`os.stat` signature changes.
  17. Note that no external programs are called from this function, giving it
  18. portability and efficiency.
  19. .. function:: cmpfiles(dir1, dir2, common[, shallow])
  20. Compare the files in the two directories *dir1* and *dir2* whose names are
  21. given by *common*.
  22. Returns three lists of file names: *match*, *mismatch*,
  23. *errors*. *match* contains the list of files that match, *mismatch* contains
  24. the names of those that don't, and *errors* lists the names of files which
  25. could not be compared. Files are listed in *errors* if they don't exist in
  26. one of the directories, the user lacks permission to read them or if the
  27. comparison could not be done for some other reason.
  28. The *shallow* parameter has the same meaning and default value as for
  29. :func:`filecmp.cmp`.
  30. For example, ``cmpfiles('a', 'b', ['c', 'd/e'])`` will compare ``a/c`` with
  31. ``b/c`` and ``a/d/e`` with ``b/d/e``. ``'c'`` and ``'d/e'`` will each be in
  32. one of the three returned lists.
  33. Example::
  34. >>> import filecmp
  35. >>> filecmp.cmp('undoc.rst', 'undoc.rst')
  36. True
  37. >>> filecmp.cmp('undoc.rst', 'index.rst')
  38. False
  39. .. _dircmp-objects:
  40. The :class:`dircmp` class
  41. -------------------------
  42. :class:`dircmp` instances are built using this constructor:
  43. .. class:: dircmp(a, b[, ignore[, hide]])
  44. Construct a new directory comparison object, to compare the directories *a* and
  45. *b*. *ignore* is a list of names to ignore, and defaults to ``['RCS', 'CVS',
  46. 'tags']``. *hide* is a list of names to hide, and defaults to ``[os.curdir,
  47. os.pardir]``.
  48. The :class:`dircmp` class provides the following methods:
  49. .. method:: report()
  50. Print (to ``sys.stdout``) a comparison between *a* and *b*.
  51. .. method:: report_partial_closure()
  52. Print a comparison between *a* and *b* and common immediate
  53. subdirectories.
  54. .. method:: report_full_closure()
  55. Print a comparison between *a* and *b* and common subdirectories
  56. (recursively).
  57. The :class:`dircmp` offers a number of interesting attributes that may be
  58. used to get various bits of information about the directory trees being
  59. compared.
  60. Note that via :meth:`__getattr__` hooks, all attributes are computed lazily,
  61. so there is no speed penalty if only those attributes which are lightweight
  62. to compute are used.
  63. .. attribute:: left_list
  64. Files and subdirectories in *a*, filtered by *hide* and *ignore*.
  65. .. attribute:: right_list
  66. Files and subdirectories in *b*, filtered by *hide* and *ignore*.
  67. .. attribute:: common
  68. Files and subdirectories in both *a* and *b*.
  69. .. attribute:: left_only
  70. Files and subdirectories only in *a*.
  71. .. attribute:: right_only
  72. Files and subdirectories only in *b*.
  73. .. attribute:: common_dirs
  74. Subdirectories in both *a* and *b*.
  75. .. attribute:: common_files
  76. Files in both *a* and *b*
  77. .. attribute:: common_funny
  78. Names in both *a* and *b*, such that the type differs between the
  79. directories, or names for which :func:`os.stat` reports an error.
  80. .. attribute:: same_files
  81. Files which are identical in both *a* and *b*.
  82. .. attribute:: diff_files
  83. Files which are in both *a* and *b*, whose contents differ.
  84. .. attribute:: funny_files
  85. Files which are in both *a* and *b*, but could not be compared.
  86. .. attribute:: subdirs
  87. A dictionary mapping names in :attr:`common_dirs` to :class:`dircmp` objects.