blob: 25d0701af379336321e2370d9ceb11d55474e3ac [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001:mod:`filecmp` --- File and Directory Comparisons
2=================================================
3
4.. module:: filecmp
5 :synopsis: Compare files efficiently.
6.. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
7
Éric Araujo29a0b572011-08-19 02:14:03 +02008**Source code:** :source:`Lib/filecmp.py`
9
10--------------
Georg Brandl8ec7f652007-08-15 14:28:01 +000011
12The :mod:`filecmp` module defines functions to compare files and directories,
Mark Summerfield0752d202007-10-19 12:48:17 +000013with various optional time/correctness trade-offs. For comparing files,
14see also the :mod:`difflib` module.
Georg Brandl8ec7f652007-08-15 14:28:01 +000015
16The :mod:`filecmp` module defines the following functions:
17
18
19.. function:: cmp(f1, f2[, shallow])
20
21 Compare the files named *f1* and *f2*, returning ``True`` if they seem equal,
22 ``False`` otherwise.
23
24 Unless *shallow* is given and is false, files with identical :func:`os.stat`
25 signatures are taken to be equal.
26
27 Files that were compared using this function will not be compared again unless
28 their :func:`os.stat` signature changes.
29
30 Note that no external programs are called from this function, giving it
31 portability and efficiency.
32
33
34.. function:: cmpfiles(dir1, dir2, common[, shallow])
35
Georg Brandlee6361f2009-02-27 16:39:26 +000036 Compare the files in the two directories *dir1* and *dir2* whose names are
37 given by *common*.
Georg Brandl8ec7f652007-08-15 14:28:01 +000038
Georg Brandlee6361f2009-02-27 16:39:26 +000039 Returns three lists of file names: *match*, *mismatch*,
40 *errors*. *match* contains the list of files that match, *mismatch* contains
41 the names of those that don't, and *errors* lists the names of files which
42 could not be compared. Files are listed in *errors* if they don't exist in
43 one of the directories, the user lacks permission to read them or if the
44 comparison could not be done for some other reason.
45
46 The *shallow* parameter has the same meaning and default value as for
Georg Brandl8ec7f652007-08-15 14:28:01 +000047 :func:`filecmp.cmp`.
48
Georg Brandlee6361f2009-02-27 16:39:26 +000049 For example, ``cmpfiles('a', 'b', ['c', 'd/e'])`` will compare ``a/c`` with
50 ``b/c`` and ``a/d/e`` with ``b/d/e``. ``'c'`` and ``'d/e'`` will each be in
51 one of the three returned lists.
52
53
Georg Brandl8ec7f652007-08-15 14:28:01 +000054Example::
55
56 >>> import filecmp
57 >>> filecmp.cmp('undoc.rst', 'undoc.rst')
58 True
59 >>> filecmp.cmp('undoc.rst', 'index.rst')
60 False
61
62
63.. _dircmp-objects:
64
65The :class:`dircmp` class
66-------------------------
67
68:class:`dircmp` instances are built using this constructor:
69
70
71.. class:: dircmp(a, b[, ignore[, hide]])
72
73 Construct a new directory comparison object, to compare the directories *a* and
74 *b*. *ignore* is a list of names to ignore, and defaults to ``['RCS', 'CVS',
75 'tags']``. *hide* is a list of names to hide, and defaults to ``[os.curdir,
76 os.pardir]``.
77
Benjamin Petersonc7b05922008-04-25 01:29:10 +000078 The :class:`dircmp` class provides the following methods:
Georg Brandl8ec7f652007-08-15 14:28:01 +000079
80
Benjamin Petersonc7b05922008-04-25 01:29:10 +000081 .. method:: report()
Georg Brandl8ec7f652007-08-15 14:28:01 +000082
Benjamin Petersonc7b05922008-04-25 01:29:10 +000083 Print (to ``sys.stdout``) a comparison between *a* and *b*.
Georg Brandl8ec7f652007-08-15 14:28:01 +000084
85
Benjamin Petersonc7b05922008-04-25 01:29:10 +000086 .. method:: report_partial_closure()
Georg Brandl8ec7f652007-08-15 14:28:01 +000087
Benjamin Petersonc7b05922008-04-25 01:29:10 +000088 Print a comparison between *a* and *b* and common immediate
89 subdirectories.
Georg Brandl8ec7f652007-08-15 14:28:01 +000090
91
Benjamin Petersonc7b05922008-04-25 01:29:10 +000092 .. method:: report_full_closure()
Georg Brandl8ec7f652007-08-15 14:28:01 +000093
Benjamin Petersonc7b05922008-04-25 01:29:10 +000094 Print a comparison between *a* and *b* and common subdirectories
95 (recursively).
Georg Brandl8ec7f652007-08-15 14:28:01 +000096
Benjamin Petersonc7b05922008-04-25 01:29:10 +000097 The :class:`dircmp` offers a number of interesting attributes that may be
98 used to get various bits of information about the directory trees being
99 compared.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000100
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000101 Note that via :meth:`__getattr__` hooks, all attributes are computed lazily,
102 so there is no speed penalty if only those attributes which are lightweight
103 to compute are used.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000104
105
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000106 .. attribute:: left_list
Georg Brandl8ec7f652007-08-15 14:28:01 +0000107
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000108 Files and subdirectories in *a*, filtered by *hide* and *ignore*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000109
110
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000111 .. attribute:: right_list
Georg Brandl8ec7f652007-08-15 14:28:01 +0000112
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000113 Files and subdirectories in *b*, filtered by *hide* and *ignore*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000114
115
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000116 .. attribute:: common
Georg Brandl8ec7f652007-08-15 14:28:01 +0000117
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000118 Files and subdirectories in both *a* and *b*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000119
120
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000121 .. attribute:: left_only
Georg Brandl8ec7f652007-08-15 14:28:01 +0000122
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000123 Files and subdirectories only in *a*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000124
125
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000126 .. attribute:: right_only
Georg Brandl8ec7f652007-08-15 14:28:01 +0000127
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000128 Files and subdirectories only in *b*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000129
130
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000131 .. attribute:: common_dirs
Georg Brandl8ec7f652007-08-15 14:28:01 +0000132
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000133 Subdirectories in both *a* and *b*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000134
135
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000136 .. attribute:: common_files
Georg Brandl8ec7f652007-08-15 14:28:01 +0000137
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000138 Files in both *a* and *b*
Georg Brandl8ec7f652007-08-15 14:28:01 +0000139
140
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000141 .. attribute:: common_funny
Georg Brandl8ec7f652007-08-15 14:28:01 +0000142
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000143 Names in both *a* and *b*, such that the type differs between the
144 directories, or names for which :func:`os.stat` reports an error.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000145
146
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000147 .. attribute:: same_files
Georg Brandl8ec7f652007-08-15 14:28:01 +0000148
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000149 Files which are identical in both *a* and *b*.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000150
151
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000152 .. attribute:: diff_files
Georg Brandl8ec7f652007-08-15 14:28:01 +0000153
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000154 Files which are in both *a* and *b*, whose contents differ.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000155
156
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000157 .. attribute:: funny_files
Georg Brandl8ec7f652007-08-15 14:28:01 +0000158
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000159 Files which are in both *a* and *b*, but could not be compared.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000160
161
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000162 .. attribute:: subdirs
Georg Brandl8ec7f652007-08-15 14:28:01 +0000163
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000164 A dictionary mapping names in :attr:`common_dirs` to :class:`dircmp` objects.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000165