blob: 699e510438b8b94eb3f26194344e3454096a5a52 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`filecmp` --- File and Directory Comparisons
3=================================================
4
5.. module:: filecmp
6 :synopsis: Compare files efficiently.
7.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
8
9
10The :mod:`filecmp` module defines functions to compare files and directories,
Mark Summerfield0752d202007-10-19 12:48:17 +000011with various optional time/correctness trade-offs. For comparing files,
12see also the :mod:`difflib` module.
Georg Brandl8ec7f652007-08-15 14:28:01 +000013
Raymond Hettingere0e08222010-11-06 07:10:31 +000014.. seealso::
15
16 Latest version of the `filecmp Python source code
17 <http://svn.python.org/view/python/branches/release27-maint/Lib/filecmp.py?view=markup>`_
18
Georg Brandl8ec7f652007-08-15 14:28:01 +000019The :mod:`filecmp` module defines the following functions:
20
21
22.. function:: cmp(f1, f2[, shallow])
23
24 Compare the files named *f1* and *f2*, returning ``True`` if they seem equal,
25 ``False`` otherwise.
26
27 Unless *shallow* is given and is false, files with identical :func:`os.stat`
28 signatures are taken to be equal.
29
30 Files that were compared using this function will not be compared again unless
31 their :func:`os.stat` signature changes.
32
33 Note that no external programs are called from this function, giving it
34 portability and efficiency.
35
36
37.. function:: cmpfiles(dir1, dir2, common[, shallow])
38
Georg Brandlee6361f2009-02-27 16:39:26 +000039 Compare the files in the two directories *dir1* and *dir2* whose names are
40 given by *common*.
Georg Brandl8ec7f652007-08-15 14:28:01 +000041
Georg Brandlee6361f2009-02-27 16:39:26 +000042 Returns three lists of file names: *match*, *mismatch*,
43 *errors*. *match* contains the list of files that match, *mismatch* contains
44 the names of those that don't, and *errors* lists the names of files which
45 could not be compared. Files are listed in *errors* if they don't exist in
46 one of the directories, the user lacks permission to read them or if the
47 comparison could not be done for some other reason.
48
49 The *shallow* parameter has the same meaning and default value as for
Georg Brandl8ec7f652007-08-15 14:28:01 +000050 :func:`filecmp.cmp`.
51
Georg Brandlee6361f2009-02-27 16:39:26 +000052 For example, ``cmpfiles('a', 'b', ['c', 'd/e'])`` will compare ``a/c`` with
53 ``b/c`` and ``a/d/e`` with ``b/d/e``. ``'c'`` and ``'d/e'`` will each be in
54 one of the three returned lists.
55
56
Georg Brandl8ec7f652007-08-15 14:28:01 +000057Example::
58
59 >>> import filecmp
60 >>> filecmp.cmp('undoc.rst', 'undoc.rst')
61 True
62 >>> filecmp.cmp('undoc.rst', 'index.rst')
63 False
64
65
66.. _dircmp-objects:
67
68The :class:`dircmp` class
69-------------------------
70
71:class:`dircmp` instances are built using this constructor:
72
73
74.. class:: dircmp(a, b[, ignore[, hide]])
75
76 Construct a new directory comparison object, to compare the directories *a* and
77 *b*. *ignore* is a list of names to ignore, and defaults to ``['RCS', 'CVS',
78 'tags']``. *hide* is a list of names to hide, and defaults to ``[os.curdir,
79 os.pardir]``.
80
Benjamin Petersonc7b05922008-04-25 01:29:10 +000081 The :class:`dircmp` class provides the following methods:
Georg Brandl8ec7f652007-08-15 14:28:01 +000082
83
Benjamin Petersonc7b05922008-04-25 01:29:10 +000084 .. method:: report()
Georg Brandl8ec7f652007-08-15 14:28:01 +000085
Benjamin Petersonc7b05922008-04-25 01:29:10 +000086 Print (to ``sys.stdout``) a comparison between *a* and *b*.
Georg Brandl8ec7f652007-08-15 14:28:01 +000087
88
Benjamin Petersonc7b05922008-04-25 01:29:10 +000089 .. method:: report_partial_closure()
Georg Brandl8ec7f652007-08-15 14:28:01 +000090
Benjamin Petersonc7b05922008-04-25 01:29:10 +000091 Print a comparison between *a* and *b* and common immediate
92 subdirectories.
Georg Brandl8ec7f652007-08-15 14:28:01 +000093
94
Benjamin Petersonc7b05922008-04-25 01:29:10 +000095 .. method:: report_full_closure()
Georg Brandl8ec7f652007-08-15 14:28:01 +000096
Benjamin Petersonc7b05922008-04-25 01:29:10 +000097 Print a comparison between *a* and *b* and common subdirectories
98 (recursively).
Georg Brandl8ec7f652007-08-15 14:28:01 +000099
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000100 The :class:`dircmp` offers a number of interesting attributes that may be
101 used to get various bits of information about the directory trees being
102 compared.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000103
Benjamin Petersonc7b05922008-04-25 01:29:10 +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 Brandl8ec7f652007-08-15 14:28:01 +0000107
108
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000109 .. attribute:: left_list
Georg Brandl8ec7f652007-08-15 14:28:01 +0000110
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000111 Files and subdirectories in *a*, filtered by *hide* and *ignore*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000112
113
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000114 .. attribute:: right_list
Georg Brandl8ec7f652007-08-15 14:28:01 +0000115
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000116 Files and subdirectories in *b*, filtered by *hide* and *ignore*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000117
118
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000119 .. attribute:: common
Georg Brandl8ec7f652007-08-15 14:28:01 +0000120
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000121 Files and subdirectories in both *a* and *b*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000122
123
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000124 .. attribute:: left_only
Georg Brandl8ec7f652007-08-15 14:28:01 +0000125
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000126 Files and subdirectories only in *a*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000127
128
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000129 .. attribute:: right_only
Georg Brandl8ec7f652007-08-15 14:28:01 +0000130
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000131 Files and subdirectories only in *b*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000132
133
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000134 .. attribute:: common_dirs
Georg Brandl8ec7f652007-08-15 14:28:01 +0000135
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000136 Subdirectories in both *a* and *b*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000137
138
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000139 .. attribute:: common_files
Georg Brandl8ec7f652007-08-15 14:28:01 +0000140
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000141 Files in both *a* and *b*
Georg Brandl8ec7f652007-08-15 14:28:01 +0000142
143
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000144 .. attribute:: common_funny
Georg Brandl8ec7f652007-08-15 14:28:01 +0000145
Benjamin Petersonc7b05922008-04-25 01:29:10 +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 Brandl8ec7f652007-08-15 14:28:01 +0000148
149
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000150 .. attribute:: same_files
Georg Brandl8ec7f652007-08-15 14:28:01 +0000151
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000152 Files which are identical in both *a* and *b*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000153
154
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000155 .. attribute:: diff_files
Georg Brandl8ec7f652007-08-15 14:28:01 +0000156
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000157 Files which are in both *a* and *b*, whose contents differ.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000158
159
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000160 .. attribute:: funny_files
Georg Brandl8ec7f652007-08-15 14:28:01 +0000161
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000162 Files which are in both *a* and *b*, but could not be compared.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000163
164
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000165 .. attribute:: subdirs
Georg Brandl8ec7f652007-08-15 14:28:01 +0000166
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000167 A dictionary mapping names in :attr:`common_dirs` to :class:`dircmp` objects.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000168