blob: f57dcceff0ad6f5de5ef49e21989ab625388bbaa [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +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
8
9The :mod:`filecmp` module defines functions to compare files and directories,
Georg Brandl9afde1c2007-11-01 20:32:30 +000010with various optional time/correctness trade-offs. For comparing files,
11see also the :mod:`difflib` module.
Georg Brandl116aa622007-08-15 14:28:22 +000012
13The :mod:`filecmp` module defines the following functions:
14
15
Georg Brandl71515ca2009-05-17 12:29:12 +000016.. function:: cmp(f1, f2, shallow=True)
Georg Brandl116aa622007-08-15 14:28:22 +000017
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
Georg Brandl71515ca2009-05-17 12:29:12 +000031.. function:: cmpfiles(dir1, dir2, common, shallow=True)
Georg Brandl116aa622007-08-15 14:28:22 +000032
Benjamin Petersone0124bd2009-03-09 21:04:33 +000033 Compare the files in the two directories *dir1* and *dir2* whose names are
34 given by *common*.
Georg Brandl116aa622007-08-15 14:28:22 +000035
Benjamin Petersone0124bd2009-03-09 21:04:33 +000036 Returns three lists of file names: *match*, *mismatch*,
37 *errors*. *match* contains the list of files that match, *mismatch* contains
38 the names of those that don't, and *errors* lists the names of files which
39 could not be compared. Files are listed in *errors* if they don't exist in
40 one of the directories, the user lacks permission to read them or if the
41 comparison could not be done for some other reason.
42
43 The *shallow* parameter has the same meaning and default value as for
Georg Brandl116aa622007-08-15 14:28:22 +000044 :func:`filecmp.cmp`.
45
Benjamin Petersone0124bd2009-03-09 21:04:33 +000046 For example, ``cmpfiles('a', 'b', ['c', 'd/e'])`` will compare ``a/c`` with
47 ``b/c`` and ``a/d/e`` with ``b/d/e``. ``'c'`` and ``'d/e'`` will each be in
48 one of the three returned lists.
49
50
Georg Brandl116aa622007-08-15 14:28:22 +000051Example::
52
53 >>> import filecmp
54 >>> filecmp.cmp('undoc.rst', 'undoc.rst')
55 True
56 >>> filecmp.cmp('undoc.rst', 'index.rst')
57 False
58
59
60.. _dircmp-objects:
61
62The :class:`dircmp` class
63-------------------------
64
65:class:`dircmp` instances are built using this constructor:
66
67
Georg Brandl71515ca2009-05-17 12:29:12 +000068.. class:: dircmp(a, b, ignore=None, hide=None)
Georg Brandl116aa622007-08-15 14:28:22 +000069
70 Construct a new directory comparison object, to compare the directories *a* and
71 *b*. *ignore* is a list of names to ignore, and defaults to ``['RCS', 'CVS',
72 'tags']``. *hide* is a list of names to hide, and defaults to ``[os.curdir,
73 os.pardir]``.
74
Benjamin Petersone41251e2008-04-25 01:59:09 +000075 The :class:`dircmp` class provides the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +000076
77
Benjamin Petersone41251e2008-04-25 01:59:09 +000078 .. method:: report()
Georg Brandl116aa622007-08-15 14:28:22 +000079
Benjamin Petersone41251e2008-04-25 01:59:09 +000080 Print (to ``sys.stdout``) a comparison between *a* and *b*.
Georg Brandl116aa622007-08-15 14:28:22 +000081
82
Benjamin Petersone41251e2008-04-25 01:59:09 +000083 .. method:: report_partial_closure()
Georg Brandl116aa622007-08-15 14:28:22 +000084
Benjamin Petersone41251e2008-04-25 01:59:09 +000085 Print a comparison between *a* and *b* and common immediate
86 subdirectories.
Georg Brandl116aa622007-08-15 14:28:22 +000087
88
Benjamin Petersone41251e2008-04-25 01:59:09 +000089 .. method:: report_full_closure()
Georg Brandl116aa622007-08-15 14:28:22 +000090
Benjamin Petersone41251e2008-04-25 01:59:09 +000091 Print a comparison between *a* and *b* and common subdirectories
92 (recursively).
Georg Brandl116aa622007-08-15 14:28:22 +000093
Benjamin Petersone41251e2008-04-25 01:59:09 +000094 The :class:`dircmp` offers a number of interesting attributes that may be
95 used to get various bits of information about the directory trees being
96 compared.
Georg Brandl116aa622007-08-15 14:28:22 +000097
Benjamin Petersone41251e2008-04-25 01:59:09 +000098 Note that via :meth:`__getattr__` hooks, all attributes are computed lazily,
99 so there is no speed penalty if only those attributes which are lightweight
100 to compute are used.
Georg Brandl116aa622007-08-15 14:28:22 +0000101
102
Benjamin Petersone41251e2008-04-25 01:59:09 +0000103 .. attribute:: left_list
Georg Brandl116aa622007-08-15 14:28:22 +0000104
Benjamin Petersone41251e2008-04-25 01:59:09 +0000105 Files and subdirectories in *a*, filtered by *hide* and *ignore*.
Georg Brandl116aa622007-08-15 14:28:22 +0000106
107
Benjamin Petersone41251e2008-04-25 01:59:09 +0000108 .. attribute:: right_list
Georg Brandl116aa622007-08-15 14:28:22 +0000109
Benjamin Petersone41251e2008-04-25 01:59:09 +0000110 Files and subdirectories in *b*, filtered by *hide* and *ignore*.
Georg Brandl116aa622007-08-15 14:28:22 +0000111
112
Benjamin Petersone41251e2008-04-25 01:59:09 +0000113 .. attribute:: common
Georg Brandl116aa622007-08-15 14:28:22 +0000114
Benjamin Petersone41251e2008-04-25 01:59:09 +0000115 Files and subdirectories in both *a* and *b*.
Georg Brandl116aa622007-08-15 14:28:22 +0000116
117
Benjamin Petersone41251e2008-04-25 01:59:09 +0000118 .. attribute:: left_only
Georg Brandl116aa622007-08-15 14:28:22 +0000119
Benjamin Petersone41251e2008-04-25 01:59:09 +0000120 Files and subdirectories only in *a*.
Georg Brandl116aa622007-08-15 14:28:22 +0000121
122
Benjamin Petersone41251e2008-04-25 01:59:09 +0000123 .. attribute:: right_only
Georg Brandl116aa622007-08-15 14:28:22 +0000124
Benjamin Petersone41251e2008-04-25 01:59:09 +0000125 Files and subdirectories only in *b*.
Georg Brandl116aa622007-08-15 14:28:22 +0000126
127
Benjamin Petersone41251e2008-04-25 01:59:09 +0000128 .. attribute:: common_dirs
Georg Brandl116aa622007-08-15 14:28:22 +0000129
Benjamin Petersone41251e2008-04-25 01:59:09 +0000130 Subdirectories in both *a* and *b*.
Georg Brandl116aa622007-08-15 14:28:22 +0000131
132
Benjamin Petersone41251e2008-04-25 01:59:09 +0000133 .. attribute:: common_files
Georg Brandl116aa622007-08-15 14:28:22 +0000134
Benjamin Petersone41251e2008-04-25 01:59:09 +0000135 Files in both *a* and *b*
Georg Brandl116aa622007-08-15 14:28:22 +0000136
137
Benjamin Petersone41251e2008-04-25 01:59:09 +0000138 .. attribute:: common_funny
Georg Brandl116aa622007-08-15 14:28:22 +0000139
Benjamin Petersone41251e2008-04-25 01:59:09 +0000140 Names in both *a* and *b*, such that the type differs between the
141 directories, or names for which :func:`os.stat` reports an error.
Georg Brandl116aa622007-08-15 14:28:22 +0000142
143
Benjamin Petersone41251e2008-04-25 01:59:09 +0000144 .. attribute:: same_files
Georg Brandl116aa622007-08-15 14:28:22 +0000145
Benjamin Petersone41251e2008-04-25 01:59:09 +0000146 Files which are identical in both *a* and *b*.
Georg Brandl116aa622007-08-15 14:28:22 +0000147
148
Benjamin Petersone41251e2008-04-25 01:59:09 +0000149 .. attribute:: diff_files
Georg Brandl116aa622007-08-15 14:28:22 +0000150
Benjamin Petersone41251e2008-04-25 01:59:09 +0000151 Files which are in both *a* and *b*, whose contents differ.
Georg Brandl116aa622007-08-15 14:28:22 +0000152
153
Benjamin Petersone41251e2008-04-25 01:59:09 +0000154 .. attribute:: funny_files
Georg Brandl116aa622007-08-15 14:28:22 +0000155
Benjamin Petersone41251e2008-04-25 01:59:09 +0000156 Files which are in both *a* and *b*, but could not be compared.
Georg Brandl116aa622007-08-15 14:28:22 +0000157
158
Benjamin Petersone41251e2008-04-25 01:59:09 +0000159 .. attribute:: subdirs
Georg Brandl116aa622007-08-15 14:28:22 +0000160
Georg Brandl71515ca2009-05-17 12:29:12 +0000161 A dictionary mapping names in :attr:`common_dirs` to :class:`dircmp`
162 objects.
Georg Brandl116aa622007-08-15 14:28:22 +0000163