Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | |
| 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 | |
| 10 | The :mod:`filecmp` module defines functions to compare files and directories, |
Mark Summerfield | 0752d20 | 2007-10-19 12:48:17 +0000 | [diff] [blame] | 11 | with various optional time/correctness trade-offs. For comparing files, |
| 12 | see also the :mod:`difflib` module. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 13 | |
Raymond Hettinger | e0e0822 | 2010-11-06 07:10:31 +0000 | [diff] [blame] | 14 | .. seealso:: |
| 15 | |
| 16 | Latest version of the `filecmp Python source code |
| 17 | <http://svn.python.org/view/python/branches/release27-maint/Lib/filecmp.py?view=markup>`_ |
| 18 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 19 | The :mod:`filecmp` module defines the following functions: |
| 20 | |
| 21 | |
| 22 | .. function:: cmp(f1, f2[, shallow]) |
| 23 | |
| 24 | Compare the files named *f1* and *f2*, returning ``True`` if they seem equal, |
| 25 | ``False`` otherwise. |
| 26 | |
| 27 | Unless *shallow* is given and is false, files with identical :func:`os.stat` |
| 28 | signatures are taken to be equal. |
| 29 | |
| 30 | Files that were compared using this function will not be compared again unless |
| 31 | their :func:`os.stat` signature changes. |
| 32 | |
| 33 | Note that no external programs are called from this function, giving it |
| 34 | portability and efficiency. |
| 35 | |
| 36 | |
| 37 | .. function:: cmpfiles(dir1, dir2, common[, shallow]) |
| 38 | |
Georg Brandl | ee6361f | 2009-02-27 16:39:26 +0000 | [diff] [blame] | 39 | Compare the files in the two directories *dir1* and *dir2* whose names are |
| 40 | given by *common*. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 41 | |
Georg Brandl | ee6361f | 2009-02-27 16:39:26 +0000 | [diff] [blame] | 42 | Returns three lists of file names: *match*, *mismatch*, |
| 43 | *errors*. *match* contains the list of files that match, *mismatch* contains |
| 44 | the names of those that don't, and *errors* lists the names of files which |
| 45 | could not be compared. Files are listed in *errors* if they don't exist in |
| 46 | one of the directories, the user lacks permission to read them or if the |
| 47 | comparison could not be done for some other reason. |
| 48 | |
| 49 | The *shallow* parameter has the same meaning and default value as for |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 50 | :func:`filecmp.cmp`. |
| 51 | |
Georg Brandl | ee6361f | 2009-02-27 16:39:26 +0000 | [diff] [blame] | 52 | For example, ``cmpfiles('a', 'b', ['c', 'd/e'])`` will compare ``a/c`` with |
| 53 | ``b/c`` and ``a/d/e`` with ``b/d/e``. ``'c'`` and ``'d/e'`` will each be in |
| 54 | one of the three returned lists. |
| 55 | |
| 56 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 57 | Example:: |
| 58 | |
| 59 | >>> import filecmp |
| 60 | >>> filecmp.cmp('undoc.rst', 'undoc.rst') |
| 61 | True |
| 62 | >>> filecmp.cmp('undoc.rst', 'index.rst') |
| 63 | False |
| 64 | |
| 65 | |
| 66 | .. _dircmp-objects: |
| 67 | |
| 68 | The :class:`dircmp` class |
| 69 | ------------------------- |
| 70 | |
| 71 | :class:`dircmp` instances are built using this constructor: |
| 72 | |
| 73 | |
| 74 | .. class:: dircmp(a, b[, ignore[, hide]]) |
| 75 | |
| 76 | Construct a new directory comparison object, to compare the directories *a* and |
| 77 | *b*. *ignore* is a list of names to ignore, and defaults to ``['RCS', 'CVS', |
| 78 | 'tags']``. *hide* is a list of names to hide, and defaults to ``[os.curdir, |
| 79 | os.pardir]``. |
| 80 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 81 | The :class:`dircmp` class provides the following methods: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 82 | |
| 83 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 84 | .. method:: report() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 85 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 86 | Print (to ``sys.stdout``) a comparison between *a* and *b*. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 87 | |
| 88 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 89 | .. method:: report_partial_closure() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 90 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 91 | Print a comparison between *a* and *b* and common immediate |
| 92 | subdirectories. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 93 | |
| 94 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 95 | .. method:: report_full_closure() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 96 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 97 | Print a comparison between *a* and *b* and common subdirectories |
| 98 | (recursively). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 99 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 100 | The :class:`dircmp` offers a number of interesting attributes that may be |
| 101 | used to get various bits of information about the directory trees being |
| 102 | compared. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 103 | |
Benjamin Peterson | c7b0592 | 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 | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 107 | |
| 108 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 109 | .. attribute:: left_list |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 110 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 111 | Files and subdirectories in *a*, filtered by *hide* and *ignore*. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 112 | |
| 113 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 114 | .. attribute:: right_list |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 115 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 116 | Files and subdirectories in *b*, filtered by *hide* and *ignore*. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 117 | |
| 118 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 119 | .. attribute:: common |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 120 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 121 | Files and subdirectories in both *a* and *b*. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 122 | |
| 123 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 124 | .. attribute:: left_only |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 125 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 126 | Files and subdirectories only in *a*. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 127 | |
| 128 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 129 | .. attribute:: right_only |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 130 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 131 | Files and subdirectories only in *b*. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 132 | |
| 133 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 134 | .. attribute:: common_dirs |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 135 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 136 | Subdirectories in both *a* and *b*. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 137 | |
| 138 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 139 | .. attribute:: common_files |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 140 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 141 | Files in both *a* and *b* |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 142 | |
| 143 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 144 | .. attribute:: common_funny |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 145 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 146 | Names in both *a* and *b*, such that the type differs between the |
| 147 | directories, or names for which :func:`os.stat` reports an error. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 148 | |
| 149 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 150 | .. attribute:: same_files |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 151 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 152 | Files which are identical in both *a* and *b*. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 153 | |
| 154 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 155 | .. attribute:: diff_files |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 156 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 157 | Files which are in both *a* and *b*, whose contents differ. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 158 | |
| 159 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 160 | .. attribute:: funny_files |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 161 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 162 | Files which are in both *a* and *b*, but could not be compared. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 163 | |
| 164 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 165 | .. attribute:: subdirs |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 166 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 167 | A dictionary mapping names in :attr:`common_dirs` to :class:`dircmp` objects. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 168 | |