blob: f84cfa9a2da076085dcb72466d448da8533fef57 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`filecmp` --- File and Directory Comparisons
2=================================================
3
4.. module:: filecmp
5 :synopsis: Compare files efficiently.
6.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
7
Raymond Hettinger10480942011-01-10 03:26:08 +00008**Source code:** :source:`Lib/filecmp.py`
Georg Brandl116aa622007-08-15 14:28:22 +00009
Raymond Hettinger4f707fd2011-01-10 19:54:11 +000010--------------
11
Georg Brandl116aa622007-08-15 14:28:22 +000012The :mod:`filecmp` module defines functions to compare files and directories,
Georg Brandl9afde1c2007-11-01 20:32:30 +000013with various optional time/correctness trade-offs. For comparing files,
14see also the :mod:`difflib` module.
Georg Brandl116aa622007-08-15 14:28:22 +000015
16The :mod:`filecmp` module defines the following functions:
17
18
Georg Brandl71515ca2009-05-17 12:29:12 +000019.. function:: cmp(f1, f2, shallow=True)
Georg Brandl116aa622007-08-15 14:28:22 +000020
21 Compare the files named *f1* and *f2*, returning ``True`` if they seem equal,
22 ``False`` otherwise.
23
24 Unless *shallow* is given and is false, files with identical :func:`os.stat`
25 signatures are taken to be equal.
26
27 Files that were compared using this function will not be compared again unless
28 their :func:`os.stat` signature changes.
29
30 Note that no external programs are called from this function, giving it
31 portability and efficiency.
32
33
Georg Brandl71515ca2009-05-17 12:29:12 +000034.. function:: cmpfiles(dir1, dir2, common, shallow=True)
Georg Brandl116aa622007-08-15 14:28:22 +000035
Benjamin Petersone0124bd2009-03-09 21:04:33 +000036 Compare the files in the two directories *dir1* and *dir2* whose names are
37 given by *common*.
Georg Brandl116aa622007-08-15 14:28:22 +000038
Benjamin Petersone0124bd2009-03-09 21:04:33 +000039 Returns three lists of file names: *match*, *mismatch*,
40 *errors*. *match* contains the list of files that match, *mismatch* contains
41 the names of those that don't, and *errors* lists the names of files which
42 could not be compared. Files are listed in *errors* if they don't exist in
43 one of the directories, the user lacks permission to read them or if the
44 comparison could not be done for some other reason.
45
46 The *shallow* parameter has the same meaning and default value as for
Georg Brandl116aa622007-08-15 14:28:22 +000047 :func:`filecmp.cmp`.
48
Benjamin Petersone0124bd2009-03-09 21:04:33 +000049 For example, ``cmpfiles('a', 'b', ['c', 'd/e'])`` will compare ``a/c`` with
50 ``b/c`` and ``a/d/e`` with ``b/d/e``. ``'c'`` and ``'d/e'`` will each be in
51 one of the three returned lists.
52
53
Georg Brandl116aa622007-08-15 14:28:22 +000054Example::
55
56 >>> import filecmp
57 >>> filecmp.cmp('undoc.rst', 'undoc.rst')
58 True
59 >>> filecmp.cmp('undoc.rst', 'index.rst')
60 False
61
62
63.. _dircmp-objects:
64
65The :class:`dircmp` class
66-------------------------
67
68:class:`dircmp` instances are built using this constructor:
69
70
Georg Brandl71515ca2009-05-17 12:29:12 +000071.. class:: dircmp(a, b, ignore=None, hide=None)
Georg Brandl116aa622007-08-15 14:28:22 +000072
73 Construct a new directory comparison object, to compare the directories *a* and
74 *b*. *ignore* is a list of names to ignore, and defaults to ``['RCS', 'CVS',
75 'tags']``. *hide* is a list of names to hide, and defaults to ``[os.curdir,
76 os.pardir]``.
77
Senthil Kumaran28a9f212012-07-22 19:12:58 -070078 The :class:`dircmp` class compares files by doing *shallow* comparisons
79 as described for :func:`filecmp.cmp`.
80
Benjamin Petersone41251e2008-04-25 01:59:09 +000081 The :class:`dircmp` class provides the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +000082
83
Benjamin Petersone41251e2008-04-25 01:59:09 +000084 .. method:: report()
Georg Brandl116aa622007-08-15 14:28:22 +000085
Benjamin Petersone41251e2008-04-25 01:59:09 +000086 Print (to ``sys.stdout``) a comparison between *a* and *b*.
Georg Brandl116aa622007-08-15 14:28:22 +000087
88
Benjamin Petersone41251e2008-04-25 01:59:09 +000089 .. method:: report_partial_closure()
Georg Brandl116aa622007-08-15 14:28:22 +000090
Benjamin Petersone41251e2008-04-25 01:59:09 +000091 Print a comparison between *a* and *b* and common immediate
92 subdirectories.
Georg Brandl116aa622007-08-15 14:28:22 +000093
94
Benjamin Petersone41251e2008-04-25 01:59:09 +000095 .. method:: report_full_closure()
Georg Brandl116aa622007-08-15 14:28:22 +000096
Benjamin Petersone41251e2008-04-25 01:59:09 +000097 Print a comparison between *a* and *b* and common subdirectories
98 (recursively).
Georg Brandl116aa622007-08-15 14:28:22 +000099
Senthil Kumaran28a9f212012-07-22 19:12:58 -0700100 The :class:`dircmp` class offers a number of interesting attributes that may be
Benjamin Petersone41251e2008-04-25 01:59:09 +0000101 used to get various bits of information about the directory trees being
102 compared.
Georg Brandl116aa622007-08-15 14:28:22 +0000103
Benjamin Petersone41251e2008-04-25 01:59:09 +0000104 Note that via :meth:`__getattr__` hooks, all attributes are computed lazily,
105 so there is no speed penalty if only those attributes which are lightweight
106 to compute are used.
Georg Brandl116aa622007-08-15 14:28:22 +0000107
108
Benjamin Petersone41251e2008-04-25 01:59:09 +0000109 .. attribute:: left_list
Georg Brandl116aa622007-08-15 14:28:22 +0000110
Benjamin Petersone41251e2008-04-25 01:59:09 +0000111 Files and subdirectories in *a*, filtered by *hide* and *ignore*.
Georg Brandl116aa622007-08-15 14:28:22 +0000112
113
Benjamin Petersone41251e2008-04-25 01:59:09 +0000114 .. attribute:: right_list
Georg Brandl116aa622007-08-15 14:28:22 +0000115
Benjamin Petersone41251e2008-04-25 01:59:09 +0000116 Files and subdirectories in *b*, filtered by *hide* and *ignore*.
Georg Brandl116aa622007-08-15 14:28:22 +0000117
118
Benjamin Petersone41251e2008-04-25 01:59:09 +0000119 .. attribute:: common
Georg Brandl116aa622007-08-15 14:28:22 +0000120
Benjamin Petersone41251e2008-04-25 01:59:09 +0000121 Files and subdirectories in both *a* and *b*.
Georg Brandl116aa622007-08-15 14:28:22 +0000122
123
Benjamin Petersone41251e2008-04-25 01:59:09 +0000124 .. attribute:: left_only
Georg Brandl116aa622007-08-15 14:28:22 +0000125
Benjamin Petersone41251e2008-04-25 01:59:09 +0000126 Files and subdirectories only in *a*.
Georg Brandl116aa622007-08-15 14:28:22 +0000127
128
Benjamin Petersone41251e2008-04-25 01:59:09 +0000129 .. attribute:: right_only
Georg Brandl116aa622007-08-15 14:28:22 +0000130
Benjamin Petersone41251e2008-04-25 01:59:09 +0000131 Files and subdirectories only in *b*.
Georg Brandl116aa622007-08-15 14:28:22 +0000132
133
Benjamin Petersone41251e2008-04-25 01:59:09 +0000134 .. attribute:: common_dirs
Georg Brandl116aa622007-08-15 14:28:22 +0000135
Benjamin Petersone41251e2008-04-25 01:59:09 +0000136 Subdirectories in both *a* and *b*.
Georg Brandl116aa622007-08-15 14:28:22 +0000137
138
Benjamin Petersone41251e2008-04-25 01:59:09 +0000139 .. attribute:: common_files
Georg Brandl116aa622007-08-15 14:28:22 +0000140
Benjamin Petersone41251e2008-04-25 01:59:09 +0000141 Files in both *a* and *b*
Georg Brandl116aa622007-08-15 14:28:22 +0000142
143
Benjamin Petersone41251e2008-04-25 01:59:09 +0000144 .. attribute:: common_funny
Georg Brandl116aa622007-08-15 14:28:22 +0000145
Benjamin Petersone41251e2008-04-25 01:59:09 +0000146 Names in both *a* and *b*, such that the type differs between the
147 directories, or names for which :func:`os.stat` reports an error.
Georg Brandl116aa622007-08-15 14:28:22 +0000148
149
Benjamin Petersone41251e2008-04-25 01:59:09 +0000150 .. attribute:: same_files
Georg Brandl116aa622007-08-15 14:28:22 +0000151
Senthil Kumaran28a9f212012-07-22 19:12:58 -0700152 Files which are identical in both *a* and *b*, using the class's
153 file comparison operator.
Georg Brandl116aa622007-08-15 14:28:22 +0000154
155
Benjamin Petersone41251e2008-04-25 01:59:09 +0000156 .. attribute:: diff_files
Georg Brandl116aa622007-08-15 14:28:22 +0000157
Senthil Kumaran28a9f212012-07-22 19:12:58 -0700158 Files which are in both *a* and *b*, whose contents differ according
159 to the class's file comparison operator.
Georg Brandl116aa622007-08-15 14:28:22 +0000160
161
Benjamin Petersone41251e2008-04-25 01:59:09 +0000162 .. attribute:: funny_files
Georg Brandl116aa622007-08-15 14:28:22 +0000163
Benjamin Petersone41251e2008-04-25 01:59:09 +0000164 Files which are in both *a* and *b*, but could not be compared.
Georg Brandl116aa622007-08-15 14:28:22 +0000165
166
Benjamin Petersone41251e2008-04-25 01:59:09 +0000167 .. attribute:: subdirs
Georg Brandl116aa622007-08-15 14:28:22 +0000168
Georg Brandl71515ca2009-05-17 12:29:12 +0000169 A dictionary mapping names in :attr:`common_dirs` to :class:`dircmp`
170 objects.
Georg Brandl116aa622007-08-15 14:28:22 +0000171