Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | :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 Araujo | b30f19f | 2011-08-19 02:14:03 +0200 | [diff] [blame] | 8 | **Source code:** :source:`Lib/filecmp.py` |
| 9 | |
| 10 | -------------- |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 11 | |
| 12 | The :mod:`filecmp` module defines functions to compare files and directories, |
Mark Summerfield | e0154c2 | 2007-10-19 12:48:17 +0000 | [diff] [blame] | 13 | with various optional time/correctness trade-offs. For comparing files, |
| 14 | see also the :mod:`difflib` module. |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 15 | |
| 16 | The :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 Brandl | cf4f238 | 2009-02-27 16:39:26 +0000 | [diff] [blame] | 36 | Compare the files in the two directories *dir1* and *dir2* whose names are |
| 37 | given by *common*. |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 38 | |
Georg Brandl | cf4f238 | 2009-02-27 16:39:26 +0000 | [diff] [blame] | 39 | 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 Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 47 | :func:`filecmp.cmp`. |
| 48 | |
Georg Brandl | cf4f238 | 2009-02-27 16:39:26 +0000 | [diff] [blame] | 49 | 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 Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 54 | Example:: |
| 55 | |
| 56 | >>> import filecmp |
Ezio Melotti | cfd85a7 | 2013-01-11 08:53:10 +0200 | [diff] [blame] | 57 | >>> filecmp.cmp('undoc.rst', 'undoc.rst') # doctest: +SKIP |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 58 | True |
Ezio Melotti | cfd85a7 | 2013-01-11 08:53:10 +0200 | [diff] [blame] | 59 | >>> filecmp.cmp('undoc.rst', 'index.rst') # doctest: +SKIP |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 60 | False |
| 61 | |
| 62 | |
| 63 | .. _dircmp-objects: |
| 64 | |
| 65 | The :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 | |
Senthil Kumaran | 8935c91 | 2012-07-22 19:36:03 -0700 | [diff] [blame] | 78 | The :class:`dircmp` class compares files by doing *shallow* comparisons |
| 79 | as described for :func:`filecmp.cmp`. |
| 80 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 81 | The :class:`dircmp` class provides the following methods: |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 82 | |
| 83 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 84 | .. method:: report() |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 85 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 86 | Print (to ``sys.stdout``) a comparison between *a* and *b*. |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 87 | |
| 88 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 89 | .. method:: report_partial_closure() |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 90 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 91 | Print a comparison between *a* and *b* and common immediate |
| 92 | subdirectories. |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 93 | |
| 94 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 95 | .. method:: report_full_closure() |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 96 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 97 | Print a comparison between *a* and *b* and common subdirectories |
| 98 | (recursively). |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 99 | |
Senthil Kumaran | 8935c91 | 2012-07-22 19:36:03 -0700 | [diff] [blame] | 100 | The :class:`dircmp` class offers a number of interesting attributes that may be |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 101 | used to get various bits of information about the directory trees being |
| 102 | compared. |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 103 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 104 | Note that via :meth:`__getattr__` hooks, all attributes are computed lazily, |
| 105 | so there is no speed penalty if only those attributes which are lightweight |
| 106 | to compute are used. |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 107 | |
| 108 | |
R David Murray | d5fef65 | 2012-08-14 21:50:38 -0400 | [diff] [blame] | 109 | .. attribute:: left |
| 110 | |
| 111 | The directory *a*. |
| 112 | |
| 113 | |
| 114 | .. attribute:: right |
| 115 | |
| 116 | The directory *b*. |
| 117 | |
| 118 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 119 | .. attribute:: left_list |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 120 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 121 | Files and subdirectories in *a*, filtered by *hide* and *ignore*. |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 122 | |
| 123 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 124 | .. attribute:: right_list |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 125 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 126 | Files and subdirectories in *b*, filtered by *hide* and *ignore*. |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 127 | |
| 128 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 129 | .. attribute:: common |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 130 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 131 | Files and subdirectories in both *a* and *b*. |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 132 | |
| 133 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 134 | .. attribute:: left_only |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 135 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 136 | Files and subdirectories only in *a*. |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 137 | |
| 138 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 139 | .. attribute:: right_only |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 140 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 141 | Files and subdirectories only in *b*. |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 142 | |
| 143 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 144 | .. attribute:: common_dirs |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 145 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 146 | Subdirectories in both *a* and *b*. |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 147 | |
| 148 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 149 | .. attribute:: common_files |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 150 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 151 | Files in both *a* and *b* |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 152 | |
| 153 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 154 | .. attribute:: common_funny |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 155 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 156 | Names in both *a* and *b*, such that the type differs between the |
| 157 | directories, or names for which :func:`os.stat` reports an error. |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 158 | |
| 159 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 160 | .. attribute:: same_files |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 161 | |
Senthil Kumaran | 8935c91 | 2012-07-22 19:36:03 -0700 | [diff] [blame] | 162 | Files which are identical in both *a* and *b*, using the class's |
| 163 | file comparison operator. |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 164 | |
| 165 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 166 | .. attribute:: diff_files |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 167 | |
Senthil Kumaran | 8935c91 | 2012-07-22 19:36:03 -0700 | [diff] [blame] | 168 | Files which are in both *a* and *b*, whose contents differ according |
| 169 | to the class's file comparison operator. |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 170 | |
| 171 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 172 | .. attribute:: funny_files |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 173 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 174 | Files which are in both *a* and *b*, but could not be compared. |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 175 | |
| 176 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 177 | .. attribute:: subdirs |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 178 | |
Benjamin Peterson | 49e8c70 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 179 | A dictionary mapping names in :attr:`common_dirs` to :class:`dircmp` objects. |
Georg Brandl | 4c454ab | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 180 | |
R David Murray | d5fef65 | 2012-08-14 21:50:38 -0400 | [diff] [blame] | 181 | |
| 182 | Here is a simplified example of using the ``subdirs`` attribute to search |
| 183 | recursively through two directories to show common different files:: |
| 184 | |
| 185 | >>> from filecmp import dircmp |
| 186 | >>> def print_diff_files(dcmp): |
| 187 | ... for name in dcmp.diff_files: |
| 188 | ... print "diff_file %s found in %s and %s" % (name, dcmp.left, |
| 189 | ... dcmp.right) |
| 190 | ... for sub_dcmp in dcmp.subdirs.values(): |
| 191 | ... print_diff_files(sub_dcmp) |
| 192 | ... |
Ezio Melotti | cfd85a7 | 2013-01-11 08:53:10 +0200 | [diff] [blame] | 193 | >>> dcmp = dircmp('dir1', 'dir2') # doctest: +SKIP |
| 194 | >>> print_diff_files(dcmp) # doctest: +SKIP |
R David Murray | d5fef65 | 2012-08-14 21:50:38 -0400 | [diff] [blame] | 195 | |