• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1:mod:`filecmp` --- File and Directory Comparisons
2=================================================
3
4.. module:: filecmp
5   :synopsis: Compare files efficiently.
6
7.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
8
9**Source code:** :source:`Lib/filecmp.py`
10
11--------------
12
13The :mod:`filecmp` module defines functions to compare files and directories,
14with various optional time/correctness trade-offs. For comparing files,
15see also the :mod:`difflib` module.
16
17The :mod:`filecmp` module defines the following functions:
18
19
20.. function:: cmp(f1, f2, shallow=True)
21
22   Compare the files named *f1* and *f2*, returning ``True`` if they seem equal,
23   ``False`` otherwise.
24
25   If *shallow* is true and the :func:`os.stat` signatures (file type, size, and
26   modification time) of both files are identical, the files are taken to be
27   equal.
28
29   Otherwise, the files are treated as different if their sizes or contents differ.
30
31   Note that no external programs are called from this function, giving it
32   portability and efficiency.
33
34   This function uses a cache for past comparisons and the results,
35   with cache entries invalidated if the :func:`os.stat` information for the
36   file changes.  The entire cache may be cleared using :func:`clear_cache`.
37
38
39.. function:: cmpfiles(dir1, dir2, common, shallow=True)
40
41   Compare the files in the two directories *dir1* and *dir2* whose names are
42   given by *common*.
43
44   Returns three lists of file names: *match*, *mismatch*,
45   *errors*.  *match* contains the list of files that match, *mismatch* contains
46   the names of those that don't, and *errors* lists the names of files which
47   could not be compared.  Files are listed in *errors* if they don't exist in
48   one of the directories, the user lacks permission to read them or if the
49   comparison could not be done for some other reason.
50
51   The *shallow* parameter has the same meaning and default value as for
52   :func:`filecmp.cmp`.
53
54   For example, ``cmpfiles('a', 'b', ['c', 'd/e'])`` will compare ``a/c`` with
55   ``b/c`` and ``a/d/e`` with ``b/d/e``.  ``'c'`` and ``'d/e'`` will each be in
56   one of the three returned lists.
57
58
59.. function:: clear_cache()
60
61   Clear the filecmp cache. This may be useful if a file is compared so quickly
62   after it is modified that it is within the mtime resolution of
63   the underlying filesystem.
64
65   .. versionadded:: 3.4
66
67
68.. _dircmp-objects:
69
70The :class:`dircmp` class
71-------------------------
72
73.. class:: dircmp(a, b, ignore=None, hide=None)
74
75   Construct a new directory comparison object, to compare the directories *a*
76   and *b*.  *ignore* is a list of names to ignore, and defaults to
77   :attr:`filecmp.DEFAULT_IGNORES`.  *hide* is a list of names to hide, and
78   defaults to ``[os.curdir, os.pardir]``.
79
80   The :class:`dircmp` class compares files by doing *shallow* comparisons
81   as described for :func:`filecmp.cmp`.
82
83   The :class:`dircmp` class provides the following methods:
84
85   .. method:: report()
86
87      Print (to :data:`sys.stdout`) a comparison between *a* and *b*.
88
89   .. method:: report_partial_closure()
90
91      Print a comparison between *a* and *b* and common immediate
92      subdirectories.
93
94   .. method:: report_full_closure()
95
96      Print a comparison between *a* and *b* and common subdirectories
97      (recursively).
98
99   The :class:`dircmp` class offers a number of interesting attributes that may be
100   used to get various bits of information about the directory trees being
101   compared.
102
103   Note that via :meth:`__getattr__` hooks, all attributes are computed lazily,
104   so there is no speed penalty if only those attributes which are lightweight
105   to compute are used.
106
107
108   .. attribute:: left
109
110      The directory *a*.
111
112
113   .. attribute:: right
114
115      The directory *b*.
116
117
118   .. attribute:: left_list
119
120      Files and subdirectories in *a*, filtered by *hide* and *ignore*.
121
122
123   .. attribute:: right_list
124
125      Files and subdirectories in *b*, filtered by *hide* and *ignore*.
126
127
128   .. attribute:: common
129
130      Files and subdirectories in both *a* and *b*.
131
132
133   .. attribute:: left_only
134
135      Files and subdirectories only in *a*.
136
137
138   .. attribute:: right_only
139
140      Files and subdirectories only in *b*.
141
142
143   .. attribute:: common_dirs
144
145      Subdirectories in both *a* and *b*.
146
147
148   .. attribute:: common_files
149
150      Files in both *a* and *b*.
151
152
153   .. attribute:: common_funny
154
155      Names in both *a* and *b*, such that the type differs between the
156      directories, or names for which :func:`os.stat` reports an error.
157
158
159   .. attribute:: same_files
160
161      Files which are identical in both *a* and *b*, using the class's
162      file comparison operator.
163
164
165   .. attribute:: diff_files
166
167      Files which are in both *a* and *b*, whose contents differ according
168      to the class's file comparison operator.
169
170
171   .. attribute:: funny_files
172
173      Files which are in both *a* and *b*, but could not be compared.
174
175
176   .. attribute:: subdirs
177
178      A dictionary mapping names in :attr:`common_dirs` to :class:`dircmp`
179      instances (or MyDirCmp instances if this instance is of type MyDirCmp, a
180      subclass of :class:`dircmp`).
181
182      .. versionchanged:: 3.10
183         Previously entries were always :class:`dircmp` instances. Now entries
184         are the same type as *self*, if *self* is a subclass of
185         :class:`dircmp`.
186
187.. attribute:: DEFAULT_IGNORES
188
189   .. versionadded:: 3.4
190
191   List of directories ignored by :class:`dircmp` by default.
192
193
194Here is a simplified example of using the ``subdirs`` attribute to search
195recursively through two directories to show common different files::
196
197    >>> from filecmp import dircmp
198    >>> def print_diff_files(dcmp):
199    ...     for name in dcmp.diff_files:
200    ...         print("diff_file %s found in %s and %s" % (name, dcmp.left,
201    ...               dcmp.right))
202    ...     for sub_dcmp in dcmp.subdirs.values():
203    ...         print_diff_files(sub_dcmp)
204    ...
205    >>> dcmp = dircmp('dir1', 'dir2') # doctest: +SKIP
206    >>> print_diff_files(dcmp) # doctest: +SKIP
207
208