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