blob: 95c4ddf267141c9eb466e251af576e6b92d426a0 [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
Eli Benderskye431ed22012-07-24 19:47:34 +030024 If *shallow* is true, files with identical :func:`os.stat` signatures are
25 taken to be equal. Otherwise, the contents of the files are compared.
Georg Brandl116aa622007-08-15 14:28:22 +000026
27 Note that no external programs are called from this function, giving it
28 portability and efficiency.
29
30
Georg Brandl71515ca2009-05-17 12:29:12 +000031.. function:: cmpfiles(dir1, dir2, common, shallow=True)
Georg Brandl116aa622007-08-15 14:28:22 +000032
Benjamin Petersone0124bd2009-03-09 21:04:33 +000033 Compare the files in the two directories *dir1* and *dir2* whose names are
34 given by *common*.
Georg Brandl116aa622007-08-15 14:28:22 +000035
Benjamin Petersone0124bd2009-03-09 21:04:33 +000036 Returns three lists of file names: *match*, *mismatch*,
37 *errors*. *match* contains the list of files that match, *mismatch* contains
38 the names of those that don't, and *errors* lists the names of files which
39 could not be compared. Files are listed in *errors* if they don't exist in
40 one of the directories, the user lacks permission to read them or if the
41 comparison could not be done for some other reason.
42
43 The *shallow* parameter has the same meaning and default value as for
Georg Brandl116aa622007-08-15 14:28:22 +000044 :func:`filecmp.cmp`.
45
Benjamin Petersone0124bd2009-03-09 21:04:33 +000046 For example, ``cmpfiles('a', 'b', ['c', 'd/e'])`` will compare ``a/c`` with
47 ``b/c`` and ``a/d/e`` with ``b/d/e``. ``'c'`` and ``'d/e'`` will each be in
48 one of the three returned lists.
49
50
Georg Brandl116aa622007-08-15 14:28:22 +000051.. _dircmp-objects:
52
53The :class:`dircmp` class
54-------------------------
55
Georg Brandl71515ca2009-05-17 12:29:12 +000056.. class:: dircmp(a, b, ignore=None, hide=None)
Georg Brandl116aa622007-08-15 14:28:22 +000057
Eli Benderskyeb2884a2013-01-12 06:13:32 -080058 Construct a new directory comparison object, to compare the directories *a*
Eli Benderskyf50d6bc2013-03-14 14:39:51 -070059 and *b*. *ignore* is a list of names to ignore, and defaults to
60 :attr:`filecmp.DEFAULT_IGNORES`. *hide* is a list of names to hide, and
Eli Benderskyeb2884a2013-01-12 06:13:32 -080061 defaults to ``[os.curdir, os.pardir]``.
Georg Brandl116aa622007-08-15 14:28:22 +000062
Senthil Kumaran28a9f212012-07-22 19:12:58 -070063 The :class:`dircmp` class compares files by doing *shallow* comparisons
64 as described for :func:`filecmp.cmp`.
65
Benjamin Petersone41251e2008-04-25 01:59:09 +000066 The :class:`dircmp` class provides the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +000067
Benjamin Petersone41251e2008-04-25 01:59:09 +000068 .. method:: report()
Georg Brandl116aa622007-08-15 14:28:22 +000069
Eli Benderskyf7a54a02012-07-24 20:44:48 +030070 Print (to :data:`sys.stdout`) a comparison between *a* and *b*.
Georg Brandl116aa622007-08-15 14:28:22 +000071
Benjamin Petersone41251e2008-04-25 01:59:09 +000072 .. method:: report_partial_closure()
Georg Brandl116aa622007-08-15 14:28:22 +000073
Benjamin Petersone41251e2008-04-25 01:59:09 +000074 Print a comparison between *a* and *b* and common immediate
75 subdirectories.
Georg Brandl116aa622007-08-15 14:28:22 +000076
Benjamin Petersone41251e2008-04-25 01:59:09 +000077 .. method:: report_full_closure()
Georg Brandl116aa622007-08-15 14:28:22 +000078
Benjamin Petersone41251e2008-04-25 01:59:09 +000079 Print a comparison between *a* and *b* and common subdirectories
80 (recursively).
Georg Brandl116aa622007-08-15 14:28:22 +000081
Senthil Kumaran28a9f212012-07-22 19:12:58 -070082 The :class:`dircmp` class offers a number of interesting attributes that may be
Benjamin Petersone41251e2008-04-25 01:59:09 +000083 used to get various bits of information about the directory trees being
84 compared.
Georg Brandl116aa622007-08-15 14:28:22 +000085
Benjamin Petersone41251e2008-04-25 01:59:09 +000086 Note that via :meth:`__getattr__` hooks, all attributes are computed lazily,
87 so there is no speed penalty if only those attributes which are lightweight
88 to compute are used.
Georg Brandl116aa622007-08-15 14:28:22 +000089
90
R David Murray2b209cd2012-08-14 21:40:13 -040091 .. attribute:: left
92
93 The directory *a*.
94
95
96 .. attribute:: right
97
98 The directory *b*.
99
100
Benjamin Petersone41251e2008-04-25 01:59:09 +0000101 .. attribute:: left_list
Georg Brandl116aa622007-08-15 14:28:22 +0000102
Benjamin Petersone41251e2008-04-25 01:59:09 +0000103 Files and subdirectories in *a*, filtered by *hide* and *ignore*.
Georg Brandl116aa622007-08-15 14:28:22 +0000104
105
Benjamin Petersone41251e2008-04-25 01:59:09 +0000106 .. attribute:: right_list
Georg Brandl116aa622007-08-15 14:28:22 +0000107
Benjamin Petersone41251e2008-04-25 01:59:09 +0000108 Files and subdirectories in *b*, filtered by *hide* and *ignore*.
Georg Brandl116aa622007-08-15 14:28:22 +0000109
110
Benjamin Petersone41251e2008-04-25 01:59:09 +0000111 .. attribute:: common
Georg Brandl116aa622007-08-15 14:28:22 +0000112
Benjamin Petersone41251e2008-04-25 01:59:09 +0000113 Files and subdirectories in both *a* and *b*.
Georg Brandl116aa622007-08-15 14:28:22 +0000114
115
Benjamin Petersone41251e2008-04-25 01:59:09 +0000116 .. attribute:: left_only
Georg Brandl116aa622007-08-15 14:28:22 +0000117
Benjamin Petersone41251e2008-04-25 01:59:09 +0000118 Files and subdirectories only in *a*.
Georg Brandl116aa622007-08-15 14:28:22 +0000119
120
Benjamin Petersone41251e2008-04-25 01:59:09 +0000121 .. attribute:: right_only
Georg Brandl116aa622007-08-15 14:28:22 +0000122
Benjamin Petersone41251e2008-04-25 01:59:09 +0000123 Files and subdirectories only in *b*.
Georg Brandl116aa622007-08-15 14:28:22 +0000124
125
Benjamin Petersone41251e2008-04-25 01:59:09 +0000126 .. attribute:: common_dirs
Georg Brandl116aa622007-08-15 14:28:22 +0000127
Benjamin Petersone41251e2008-04-25 01:59:09 +0000128 Subdirectories in both *a* and *b*.
Georg Brandl116aa622007-08-15 14:28:22 +0000129
130
Benjamin Petersone41251e2008-04-25 01:59:09 +0000131 .. attribute:: common_files
Georg Brandl116aa622007-08-15 14:28:22 +0000132
Eli Benderskyf50d6bc2013-03-14 14:39:51 -0700133 Files in both *a* and *b*.
Georg Brandl116aa622007-08-15 14:28:22 +0000134
135
Benjamin Petersone41251e2008-04-25 01:59:09 +0000136 .. attribute:: common_funny
Georg Brandl116aa622007-08-15 14:28:22 +0000137
Benjamin Petersone41251e2008-04-25 01:59:09 +0000138 Names in both *a* and *b*, such that the type differs between the
139 directories, or names for which :func:`os.stat` reports an error.
Georg Brandl116aa622007-08-15 14:28:22 +0000140
141
Benjamin Petersone41251e2008-04-25 01:59:09 +0000142 .. attribute:: same_files
Georg Brandl116aa622007-08-15 14:28:22 +0000143
Senthil Kumaran28a9f212012-07-22 19:12:58 -0700144 Files which are identical in both *a* and *b*, using the class's
145 file comparison operator.
Georg Brandl116aa622007-08-15 14:28:22 +0000146
147
Benjamin Petersone41251e2008-04-25 01:59:09 +0000148 .. attribute:: diff_files
Georg Brandl116aa622007-08-15 14:28:22 +0000149
Senthil Kumaran28a9f212012-07-22 19:12:58 -0700150 Files which are in both *a* and *b*, whose contents differ according
151 to the class's file comparison operator.
Georg Brandl116aa622007-08-15 14:28:22 +0000152
153
Benjamin Petersone41251e2008-04-25 01:59:09 +0000154 .. attribute:: funny_files
Georg Brandl116aa622007-08-15 14:28:22 +0000155
Benjamin Petersone41251e2008-04-25 01:59:09 +0000156 Files which are in both *a* and *b*, but could not be compared.
Georg Brandl116aa622007-08-15 14:28:22 +0000157
158
Benjamin Petersone41251e2008-04-25 01:59:09 +0000159 .. attribute:: subdirs
Georg Brandl116aa622007-08-15 14:28:22 +0000160
Georg Brandl71515ca2009-05-17 12:29:12 +0000161 A dictionary mapping names in :attr:`common_dirs` to :class:`dircmp`
162 objects.
Georg Brandl116aa622007-08-15 14:28:22 +0000163
Eli Benderskyeb2884a2013-01-12 06:13:32 -0800164.. attribute:: DEFAULT_IGNORES
165
Eli Benderskyabdcf2c2013-01-12 14:02:29 -0800166 .. versionadded:: 3.4
Eli Benderskyeb2884a2013-01-12 06:13:32 -0800167
168 List of directories ignored by :class:`dircmp` by default.
169
R David Murray2b209cd2012-08-14 21:40:13 -0400170
171Here is a simplified example of using the ``subdirs`` attribute to search
172recursively through two directories to show common different files::
173
174 >>> from filecmp import dircmp
175 >>> def print_diff_files(dcmp):
176 ... for name in dcmp.diff_files:
177 ... print("diff_file %s found in %s and %s" % (name, dcmp.left,
178 ... dcmp.right))
179 ... for sub_dcmp in dcmp.subdirs.values():
180 ... print_diff_files(sub_dcmp)
181 ...
Ezio Melotti40507922013-01-11 09:09:07 +0200182 >>> dcmp = dircmp('dir1', 'dir2') # doctest: +SKIP
183 >>> print_diff_files(dcmp) # doctest: +SKIP
R David Murray2b209cd2012-08-14 21:40:13 -0400184