blob: 600421412d25f422a929e37515a78d45c4c0a6f8 [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,
11with various optional time/correctness trade-offs.
12
13The :mod:`filecmp` module defines the following functions:
14
15
16.. function:: cmp(f1, f2[, shallow])
17
18 Compare the files named *f1* and *f2*, returning ``True`` if they seem equal,
19 ``False`` otherwise.
20
21 Unless *shallow* is given and is false, files with identical :func:`os.stat`
22 signatures are taken to be equal.
23
24 Files that were compared using this function will not be compared again unless
25 their :func:`os.stat` signature changes.
26
27 Note that no external programs are called from this function, giving it
28 portability and efficiency.
29
30
31.. function:: cmpfiles(dir1, dir2, common[, shallow])
32
33 Returns three lists of file names: *match*, *mismatch*, *errors*. *match*
34 contains the list of files match in both directories, *mismatch* includes the
35 names of those that don't, and *errros* lists the names of files which could not
36 be compared. Files may be listed in *errors* because the user may lack
37 permission to read them or many other reasons, but always that the comparison
38 could not be done for some reason.
39
40 The *common* parameter is a list of file names found in both directories. The
41 *shallow* parameter has the same meaning and default value as for
42 :func:`filecmp.cmp`.
43
44Example::
45
46 >>> import filecmp
47 >>> filecmp.cmp('undoc.rst', 'undoc.rst')
48 True
49 >>> filecmp.cmp('undoc.rst', 'index.rst')
50 False
51
52
53.. _dircmp-objects:
54
55The :class:`dircmp` class
56-------------------------
57
58:class:`dircmp` instances are built using this constructor:
59
60
61.. class:: dircmp(a, b[, ignore[, hide]])
62
63 Construct a new directory comparison object, to compare the directories *a* and
64 *b*. *ignore* is a list of names to ignore, and defaults to ``['RCS', 'CVS',
65 'tags']``. *hide* is a list of names to hide, and defaults to ``[os.curdir,
66 os.pardir]``.
67
68The :class:`dircmp` class provides the following methods:
69
70
71.. method:: dircmp.report()
72
73 Print (to ``sys.stdout``) a comparison between *a* and *b*.
74
75
76.. method:: dircmp.report_partial_closure()
77
78 Print a comparison between *a* and *b* and common immediate subdirectories.
79
80
81.. method:: dircmp.report_full_closure()
82
83 Print a comparison between *a* and *b* and common subdirectories (recursively).
84
85The :class:`dircmp` offers a number of interesting attributes that may be used
86to get various bits of information about the directory trees being compared.
87
88Note that via :meth:`__getattr__` hooks, all attributes are computed lazily, so
89there is no speed penalty if only those attributes which are lightweight to
90compute are used.
91
92
93.. attribute:: dircmp.left_list
94
95 Files and subdirectories in *a*, filtered by *hide* and *ignore*.
96
97
98.. attribute:: dircmp.right_list
99
100 Files and subdirectories in *b*, filtered by *hide* and *ignore*.
101
102
103.. attribute:: dircmp.common
104
105 Files and subdirectories in both *a* and *b*.
106
107
108.. attribute:: dircmp.left_only
109
110 Files and subdirectories only in *a*.
111
112
113.. attribute:: dircmp.right_only
114
115 Files and subdirectories only in *b*.
116
117
118.. attribute:: dircmp.common_dirs
119
120 Subdirectories in both *a* and *b*.
121
122
123.. attribute:: dircmp.common_files
124
125 Files in both *a* and *b*
126
127
128.. attribute:: dircmp.common_funny
129
130 Names in both *a* and *b*, such that the type differs between the directories,
131 or names for which :func:`os.stat` reports an error.
132
133
134.. attribute:: dircmp.same_files
135
136 Files which are identical in both *a* and *b*.
137
138
139.. attribute:: dircmp.diff_files
140
141 Files which are in both *a* and *b*, whose contents differ.
142
143
144.. attribute:: dircmp.funny_files
145
146 Files which are in both *a* and *b*, but could not be compared.
147
148
149.. attribute:: dircmp.subdirs
150
151 A dictionary mapping names in :attr:`common_dirs` to :class:`dircmp` objects.
152