blob: 11d74ba770c766837804478933067fde47c3a7f8 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +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,
Georg Brandl9afde1c2007-11-01 20:32:30 +000011with various optional time/correctness trade-offs. For comparing files,
12see also the :mod:`difflib` module.
Georg Brandl116aa622007-08-15 14:28:22 +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
Benjamin Petersone0124bd2009-03-09 21:04:33 +000034 Compare the files in the two directories *dir1* and *dir2* whose names are
35 given by *common*.
Georg Brandl116aa622007-08-15 14:28:22 +000036
Benjamin Petersone0124bd2009-03-09 21:04:33 +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 Brandl116aa622007-08-15 14:28:22 +000045 :func:`filecmp.cmp`.
46
Benjamin Petersone0124bd2009-03-09 21:04:33 +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 Brandl116aa622007-08-15 14:28:22 +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 Petersone41251e2008-04-25 01:59:09 +000076 The :class:`dircmp` class provides the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +000077
78
Benjamin Petersone41251e2008-04-25 01:59:09 +000079 .. method:: report()
Georg Brandl116aa622007-08-15 14:28:22 +000080
Benjamin Petersone41251e2008-04-25 01:59:09 +000081 Print (to ``sys.stdout``) a comparison between *a* and *b*.
Georg Brandl116aa622007-08-15 14:28:22 +000082
83
Benjamin Petersone41251e2008-04-25 01:59:09 +000084 .. method:: report_partial_closure()
Georg Brandl116aa622007-08-15 14:28:22 +000085
Benjamin Petersone41251e2008-04-25 01:59:09 +000086 Print a comparison between *a* and *b* and common immediate
87 subdirectories.
Georg Brandl116aa622007-08-15 14:28:22 +000088
89
Benjamin Petersone41251e2008-04-25 01:59:09 +000090 .. method:: report_full_closure()
Georg Brandl116aa622007-08-15 14:28:22 +000091
Benjamin Petersone41251e2008-04-25 01:59:09 +000092 Print a comparison between *a* and *b* and common subdirectories
93 (recursively).
Georg Brandl116aa622007-08-15 14:28:22 +000094
Benjamin Petersone41251e2008-04-25 01:59:09 +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 Brandl116aa622007-08-15 14:28:22 +000098
Benjamin Petersone41251e2008-04-25 01:59:09 +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 Brandl116aa622007-08-15 14:28:22 +0000102
103
Benjamin Petersone41251e2008-04-25 01:59:09 +0000104 .. attribute:: left_list
Georg Brandl116aa622007-08-15 14:28:22 +0000105
Benjamin Petersone41251e2008-04-25 01:59:09 +0000106 Files and subdirectories in *a*, filtered by *hide* and *ignore*.
Georg Brandl116aa622007-08-15 14:28:22 +0000107
108
Benjamin Petersone41251e2008-04-25 01:59:09 +0000109 .. attribute:: right_list
Georg Brandl116aa622007-08-15 14:28:22 +0000110
Benjamin Petersone41251e2008-04-25 01:59:09 +0000111 Files and subdirectories in *b*, filtered by *hide* and *ignore*.
Georg Brandl116aa622007-08-15 14:28:22 +0000112
113
Benjamin Petersone41251e2008-04-25 01:59:09 +0000114 .. attribute:: common
Georg Brandl116aa622007-08-15 14:28:22 +0000115
Benjamin Petersone41251e2008-04-25 01:59:09 +0000116 Files and subdirectories in both *a* and *b*.
Georg Brandl116aa622007-08-15 14:28:22 +0000117
118
Benjamin Petersone41251e2008-04-25 01:59:09 +0000119 .. attribute:: left_only
Georg Brandl116aa622007-08-15 14:28:22 +0000120
Benjamin Petersone41251e2008-04-25 01:59:09 +0000121 Files and subdirectories only in *a*.
Georg Brandl116aa622007-08-15 14:28:22 +0000122
123
Benjamin Petersone41251e2008-04-25 01:59:09 +0000124 .. attribute:: right_only
Georg Brandl116aa622007-08-15 14:28:22 +0000125
Benjamin Petersone41251e2008-04-25 01:59:09 +0000126 Files and subdirectories only in *b*.
Georg Brandl116aa622007-08-15 14:28:22 +0000127
128
Benjamin Petersone41251e2008-04-25 01:59:09 +0000129 .. attribute:: common_dirs
Georg Brandl116aa622007-08-15 14:28:22 +0000130
Benjamin Petersone41251e2008-04-25 01:59:09 +0000131 Subdirectories in both *a* and *b*.
Georg Brandl116aa622007-08-15 14:28:22 +0000132
133
Benjamin Petersone41251e2008-04-25 01:59:09 +0000134 .. attribute:: common_files
Georg Brandl116aa622007-08-15 14:28:22 +0000135
Benjamin Petersone41251e2008-04-25 01:59:09 +0000136 Files in both *a* and *b*
Georg Brandl116aa622007-08-15 14:28:22 +0000137
138
Benjamin Petersone41251e2008-04-25 01:59:09 +0000139 .. attribute:: common_funny
Georg Brandl116aa622007-08-15 14:28:22 +0000140
Benjamin Petersone41251e2008-04-25 01:59:09 +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 Brandl116aa622007-08-15 14:28:22 +0000143
144
Benjamin Petersone41251e2008-04-25 01:59:09 +0000145 .. attribute:: same_files
Georg Brandl116aa622007-08-15 14:28:22 +0000146
Benjamin Petersone41251e2008-04-25 01:59:09 +0000147 Files which are identical in both *a* and *b*.
Georg Brandl116aa622007-08-15 14:28:22 +0000148
149
Benjamin Petersone41251e2008-04-25 01:59:09 +0000150 .. attribute:: diff_files
Georg Brandl116aa622007-08-15 14:28:22 +0000151
Benjamin Petersone41251e2008-04-25 01:59:09 +0000152 Files which are in both *a* and *b*, whose contents differ.
Georg Brandl116aa622007-08-15 14:28:22 +0000153
154
Benjamin Petersone41251e2008-04-25 01:59:09 +0000155 .. attribute:: funny_files
Georg Brandl116aa622007-08-15 14:28:22 +0000156
Benjamin Petersone41251e2008-04-25 01:59:09 +0000157 Files which are in both *a* and *b*, but could not be compared.
Georg Brandl116aa622007-08-15 14:28:22 +0000158
159
Benjamin Petersone41251e2008-04-25 01:59:09 +0000160 .. attribute:: subdirs
Georg Brandl116aa622007-08-15 14:28:22 +0000161
Benjamin Petersone41251e2008-04-25 01:59:09 +0000162 A dictionary mapping names in :attr:`common_dirs` to :class:`dircmp` objects.
Georg Brandl116aa622007-08-15 14:28:22 +0000163