blob: 42b4b8c428f949160e46e096df25f6c7686c742a [file] [log] [blame]
Fred Drakec7b5b3c1999-10-29 17:23:15 +00001\section{\module{filecmp} ---
Fred Drake353aaad2000-07-03 08:24:49 +00002 File and Directory Comparisons}
Fred Drakec7b5b3c1999-10-29 17:23:15 +00003
4\declaremodule{standard}{filecmp}
Fred Drake57657bc2000-12-01 15:25:23 +00005\sectionauthor{Moshe Zadka}{moshez@zadka.site.co.il}
Fred Drakec7b5b3c1999-10-29 17:23:15 +00006\modulesynopsis{Compare files efficiently.}
7
Fred Drake353aaad2000-07-03 08:24:49 +00008
Fred Drakeba7e2c82001-05-11 17:01:32 +00009The \module{filecmp} module defines functions to compare files and
10directories, with various optional time/correctness trade-offs.
Fred Drakec7b5b3c1999-10-29 17:23:15 +000011
Fred Drakeba7e2c82001-05-11 17:01:32 +000012The \module{filecmp} module defines the following functions:
Fred Drakec7b5b3c1999-10-29 17:23:15 +000013
Raymond Hettingerf3fa9462004-12-05 01:58:09 +000014\begin{funcdesc}{cmp}{f1, f2\optional{, shallow}}
Neal Norwitzd3dab2b2002-04-05 02:21:09 +000015Compare the files named \var{f1} and \var{f2}, returning \code{True} if
16they seem equal, \code{False} otherwise.
Fred Drakec7b5b3c1999-10-29 17:23:15 +000017
18Unless \var{shallow} is given and is false, files with identical
Neal Norwitz694d9b32003-02-06 21:17:17 +000019\function{os.stat()} signatures are taken to be equal.
Fred Drakec7b5b3c1999-10-29 17:23:15 +000020
21Files that were compared using this function will not be compared again
Neal Norwitz694d9b32003-02-06 21:17:17 +000022unless their \function{os.stat()} signature changes.
Fred Drakec7b5b3c1999-10-29 17:23:15 +000023
Fred Drake353aaad2000-07-03 08:24:49 +000024Note that no external programs are called from this function, giving it
Fred Drakec7b5b3c1999-10-29 17:23:15 +000025portability and efficiency.
26\end{funcdesc}
27
Fred Drake353aaad2000-07-03 08:24:49 +000028\begin{funcdesc}{cmpfiles}{dir1, dir2, common\optional{,
Raymond Hettingerf3fa9462004-12-05 01:58:09 +000029 shallow}}
Fred Drake353aaad2000-07-03 08:24:49 +000030Returns three lists of file names: \var{match}, \var{mismatch},
31\var{errors}. \var{match} contains the list of files match in both
32directories, \var{mismatch} includes the names of those that don't,
33and \var{errros} lists the names of files which could not be
34compared. Files may be listed in \var{errors} because the user may
35lack permission to read them or many other reasons, but always that
36the comparison could not be done for some reason.
37
Neal Norwitz1abca4a2002-03-20 18:55:09 +000038The \var{common} parameter is a list of file names found in both directories.
Raymond Hettingerf3fa9462004-12-05 01:58:09 +000039The \var{shallow} parameter has the same
40meaning and default value as for \function{filecmp.cmp()}.
Fred Drake353aaad2000-07-03 08:24:49 +000041\end{funcdesc}
42
Fred Drakec7b5b3c1999-10-29 17:23:15 +000043Example:
44
45\begin{verbatim}
46>>> import filecmp
47>>> filecmp.cmp('libundoc.tex', 'libundoc.tex')
Neal Norwitzd3dab2b2002-04-05 02:21:09 +000048True
Fred Drakec7b5b3c1999-10-29 17:23:15 +000049>>> filecmp.cmp('libundoc.tex', 'lib.tex')
Neal Norwitzd3dab2b2002-04-05 02:21:09 +000050False
Fred Drakec7b5b3c1999-10-29 17:23:15 +000051\end{verbatim}
Fred Drake353aaad2000-07-03 08:24:49 +000052
53
54\subsection{The \protect\class{dircmp} class \label{dircmp-objects}}
55
Fred Drakeba7e2c82001-05-11 17:01:32 +000056\class{dircmp} instances are built using this constructor:
57
Fred Drake353aaad2000-07-03 08:24:49 +000058\begin{classdesc}{dircmp}{a, b\optional{, ignore\optional{, hide}}}
59Construct a new directory comparison object, to compare the
60directories \var{a} and \var{b}. \var{ignore} is a list of names to
61ignore, and defaults to \code{['RCS', 'CVS', 'tags']}. \var{hide} is a
Skip Montanaro9483bed2001-01-18 10:44:08 +000062list of names to hide, and defaults to \code{[os.curdir, os.pardir]}.
Fred Drake353aaad2000-07-03 08:24:49 +000063\end{classdesc}
64
Fred Drakeba7e2c82001-05-11 17:01:32 +000065The \class{dircmp} class provides the following methods:
66
Fred Drake353aaad2000-07-03 08:24:49 +000067\begin{methoddesc}[dircmp]{report}{}
68Print (to \code{sys.stdout}) a comparison between \var{a} and \var{b}.
69\end{methoddesc}
70
71\begin{methoddesc}[dircmp]{report_partial_closure}{}
72Print a comparison between \var{a} and \var{b} and common immediate
Raymond Hettinger68804312005-01-01 00:28:46 +000073subdirectories.
Fred Drake353aaad2000-07-03 08:24:49 +000074\end{methoddesc}
75
76\begin{methoddesc}[dircmp]{report_full_closure}{}
77Print a comparison between \var{a} and \var{b} and common
Raymond Hettinger68804312005-01-01 00:28:46 +000078subdirectories (recursively).
Fred Drake353aaad2000-07-03 08:24:49 +000079\end{methoddesc}
80
Fred Drakeba7e2c82001-05-11 17:01:32 +000081
82The \class{dircmp} offers a number of interesting attributes that may
83be used to get various bits of information about the directory trees
84being compared.
85
86Note that via \method{__getattr__()} hooks, all attributes are
Raymond Hettinger68804312005-01-01 00:28:46 +000087computed lazily, so there is no speed penalty if only those
Fred Drakeba7e2c82001-05-11 17:01:32 +000088attributes which are lightweight to compute are used.
89
Fred Drake353aaad2000-07-03 08:24:49 +000090\begin{memberdesc}[dircmp]{left_list}
91Files and subdirectories in \var{a}, filtered by \var{hide} and
92\var{ignore}.
93\end{memberdesc}
94
95\begin{memberdesc}[dircmp]{right_list}
96Files and subdirectories in \var{b}, filtered by \var{hide} and
97\var{ignore}.
98\end{memberdesc}
99
100\begin{memberdesc}[dircmp]{common}
101Files and subdirectories in both \var{a} and \var{b}.
102\end{memberdesc}
103
104\begin{memberdesc}[dircmp]{left_only}
105Files and subdirectories only in \var{a}.
106\end{memberdesc}
107
108\begin{memberdesc}[dircmp]{right_only}
109Files and subdirectories only in \var{b}.
110\end{memberdesc}
111
112\begin{memberdesc}[dircmp]{common_dirs}
113Subdirectories in both \var{a} and \var{b}.
114\end{memberdesc}
115
116\begin{memberdesc}[dircmp]{common_files}
117Files in both \var{a} and \var{b}
118\end{memberdesc}
119
120\begin{memberdesc}[dircmp]{common_funny}
121Names in both \var{a} and \var{b}, such that the type differs between
122the directories, or names for which \function{os.stat()} reports an
123error.
124\end{memberdesc}
125
126\begin{memberdesc}[dircmp]{same_files}
127Files which are identical in both \var{a} and \var{b}.
128\end{memberdesc}
129
130\begin{memberdesc}[dircmp]{diff_files}
131Files which are in both \var{a} and \var{b}, whose contents differ.
132\end{memberdesc}
133
134\begin{memberdesc}[dircmp]{funny_files}
135Files which are in both \var{a} and \var{b}, but could not be
136compared.
137\end{memberdesc}
138
139\begin{memberdesc}[dircmp]{subdirs}
140A dictionary mapping names in \member{common_dirs} to
141\class{dircmp} objects.
142\end{memberdesc}