blob: 11d74ba770c766837804478933067fde47c3a7f8 [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
14The :mod:`filecmp` module defines the following functions:
15
16
17.. function:: cmp(f1, f2[, shallow])
18
19 Compare the files named *f1* and *f2*, returning ``True`` if they seem equal,
20 ``False`` otherwise.
21
22 Unless *shallow* is given and is false, files with identical :func:`os.stat`
23 signatures are taken to be equal.
24
25 Files that were compared using this function will not be compared again unless
26 their :func:`os.stat` signature changes.
27
28 Note that no external programs are called from this function, giving it
29 portability and efficiency.
30
31
32.. function:: cmpfiles(dir1, dir2, common[, shallow])
33
Georg Brandlee6361f2009-02-27 16:39:26 +000034 Compare the files in the two directories *dir1* and *dir2* whose names are
35 given by *common*.
Georg Brandl8ec7f652007-08-15 14:28:01 +000036
Georg Brandlee6361f2009-02-27 16:39:26 +000037 Returns three lists of file names: *match*, *mismatch*,
38 *errors*. *match* contains the list of files that match, *mismatch* contains
39 the names of those that don't, and *errors* lists the names of files which
40 could not be compared. Files are listed in *errors* if they don't exist in
41 one of the directories, the user lacks permission to read them or if the
42 comparison could not be done for some other reason.
43
44 The *shallow* parameter has the same meaning and default value as for
Georg Brandl8ec7f652007-08-15 14:28:01 +000045 :func:`filecmp.cmp`.
46
Georg Brandlee6361f2009-02-27 16:39:26 +000047 For example, ``cmpfiles('a', 'b', ['c', 'd/e'])`` will compare ``a/c`` with
48 ``b/c`` and ``a/d/e`` with ``b/d/e``. ``'c'`` and ``'d/e'`` will each be in
49 one of the three returned lists.
50
51
Georg Brandl8ec7f652007-08-15 14:28:01 +000052Example::
53
54 >>> import filecmp
55 >>> filecmp.cmp('undoc.rst', 'undoc.rst')
56 True
57 >>> filecmp.cmp('undoc.rst', 'index.rst')
58 False
59
60
61.. _dircmp-objects:
62
63The :class:`dircmp` class
64-------------------------
65
66:class:`dircmp` instances are built using this constructor:
67
68
69.. class:: dircmp(a, b[, ignore[, hide]])
70
71 Construct a new directory comparison object, to compare the directories *a* and
72 *b*. *ignore* is a list of names to ignore, and defaults to ``['RCS', 'CVS',
73 'tags']``. *hide* is a list of names to hide, and defaults to ``[os.curdir,
74 os.pardir]``.
75
Benjamin Petersonc7b05922008-04-25 01:29:10 +000076 The :class:`dircmp` class provides the following methods:
Georg Brandl8ec7f652007-08-15 14:28:01 +000077
78
Benjamin Petersonc7b05922008-04-25 01:29:10 +000079 .. method:: report()
Georg Brandl8ec7f652007-08-15 14:28:01 +000080
Benjamin Petersonc7b05922008-04-25 01:29:10 +000081 Print (to ``sys.stdout``) a comparison between *a* and *b*.
Georg Brandl8ec7f652007-08-15 14:28:01 +000082
83
Benjamin Petersonc7b05922008-04-25 01:29:10 +000084 .. method:: report_partial_closure()
Georg Brandl8ec7f652007-08-15 14:28:01 +000085
Benjamin Petersonc7b05922008-04-25 01:29:10 +000086 Print a comparison between *a* and *b* and common immediate
87 subdirectories.
Georg Brandl8ec7f652007-08-15 14:28:01 +000088
89
Benjamin Petersonc7b05922008-04-25 01:29:10 +000090 .. method:: report_full_closure()
Georg Brandl8ec7f652007-08-15 14:28:01 +000091
Benjamin Petersonc7b05922008-04-25 01:29:10 +000092 Print a comparison between *a* and *b* and common subdirectories
93 (recursively).
Georg Brandl8ec7f652007-08-15 14:28:01 +000094
Benjamin Petersonc7b05922008-04-25 01:29:10 +000095 The :class:`dircmp` offers a number of interesting attributes that may be
96 used to get various bits of information about the directory trees being
97 compared.
Georg Brandl8ec7f652007-08-15 14:28:01 +000098
Benjamin Petersonc7b05922008-04-25 01:29:10 +000099 Note that via :meth:`__getattr__` hooks, all attributes are computed lazily,
100 so there is no speed penalty if only those attributes which are lightweight
101 to compute are used.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000102
103
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000104 .. attribute:: left_list
Georg Brandl8ec7f652007-08-15 14:28:01 +0000105
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000106 Files and subdirectories in *a*, filtered by *hide* and *ignore*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000107
108
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000109 .. attribute:: right_list
Georg Brandl8ec7f652007-08-15 14:28:01 +0000110
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000111 Files and subdirectories in *b*, filtered by *hide* and *ignore*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000112
113
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000114 .. attribute:: common
Georg Brandl8ec7f652007-08-15 14:28:01 +0000115
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000116 Files and subdirectories in both *a* and *b*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000117
118
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000119 .. attribute:: left_only
Georg Brandl8ec7f652007-08-15 14:28:01 +0000120
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000121 Files and subdirectories only in *a*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000122
123
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000124 .. attribute:: right_only
Georg Brandl8ec7f652007-08-15 14:28:01 +0000125
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000126 Files and subdirectories only in *b*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000127
128
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000129 .. attribute:: common_dirs
Georg Brandl8ec7f652007-08-15 14:28:01 +0000130
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000131 Subdirectories in both *a* and *b*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000132
133
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000134 .. attribute:: common_files
Georg Brandl8ec7f652007-08-15 14:28:01 +0000135
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000136 Files in both *a* and *b*
Georg Brandl8ec7f652007-08-15 14:28:01 +0000137
138
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000139 .. attribute:: common_funny
Georg Brandl8ec7f652007-08-15 14:28:01 +0000140
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000141 Names in both *a* and *b*, such that the type differs between the
142 directories, or names for which :func:`os.stat` reports an error.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000143
144
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000145 .. attribute:: same_files
Georg Brandl8ec7f652007-08-15 14:28:01 +0000146
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000147 Files which are identical in both *a* and *b*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000148
149
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000150 .. attribute:: diff_files
Georg Brandl8ec7f652007-08-15 14:28:01 +0000151
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000152 Files which are in both *a* and *b*, whose contents differ.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000153
154
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000155 .. attribute:: funny_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*, but could not be compared.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000158
159
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000160 .. attribute:: subdirs
Georg Brandl8ec7f652007-08-15 14:28:01 +0000161
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000162 A dictionary mapping names in :attr:`common_dirs` to :class:`dircmp` objects.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000163