blob: 7747c8cac1f9e65da82a9c4f13b0eed660daa1b7 [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
14\begin{funcdesc}{cmp}{f1, f2\optional{, shallow\optional{, use_statcache}}}
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.
20\versionchanged[\var{use_statcache} is obsolete and ignored.]{2.3}
Fred Drakec7b5b3c1999-10-29 17:23:15 +000021
22Files that were compared using this function will not be compared again
Neal Norwitz694d9b32003-02-06 21:17:17 +000023unless their \function{os.stat()} signature changes.
Fred Drakec7b5b3c1999-10-29 17:23:15 +000024
Fred Drake353aaad2000-07-03 08:24:49 +000025Note that no external programs are called from this function, giving it
Fred Drakec7b5b3c1999-10-29 17:23:15 +000026portability and efficiency.
27\end{funcdesc}
28
Fred Drake353aaad2000-07-03 08:24:49 +000029\begin{funcdesc}{cmpfiles}{dir1, dir2, common\optional{,
30 shallow\optional{, use_statcache}}}
31Returns three lists of file names: \var{match}, \var{mismatch},
32\var{errors}. \var{match} contains the list of files match in both
33directories, \var{mismatch} includes the names of those that don't,
34and \var{errros} lists the names of files which could not be
35compared. Files may be listed in \var{errors} because the user may
36lack permission to read them or many other reasons, but always that
37the comparison could not be done for some reason.
38
Neal Norwitz1abca4a2002-03-20 18:55:09 +000039The \var{common} parameter is a list of file names found in both directories.
Fred Drake353aaad2000-07-03 08:24:49 +000040The \var{shallow} and \var{use_statcache} parameters have the same
41meanings and default values as for \function{filecmp.cmp()}.
42\end{funcdesc}
43
Fred Drakec7b5b3c1999-10-29 17:23:15 +000044Example:
45
46\begin{verbatim}
47>>> import filecmp
48>>> filecmp.cmp('libundoc.tex', 'libundoc.tex')
Neal Norwitzd3dab2b2002-04-05 02:21:09 +000049True
Fred Drakec7b5b3c1999-10-29 17:23:15 +000050>>> filecmp.cmp('libundoc.tex', 'lib.tex')
Neal Norwitzd3dab2b2002-04-05 02:21:09 +000051False
Fred Drakec7b5b3c1999-10-29 17:23:15 +000052\end{verbatim}
Fred Drake353aaad2000-07-03 08:24:49 +000053
54
55\subsection{The \protect\class{dircmp} class \label{dircmp-objects}}
56
Fred Drakeba7e2c82001-05-11 17:01:32 +000057\class{dircmp} instances are built using this constructor:
58
Fred Drake353aaad2000-07-03 08:24:49 +000059\begin{classdesc}{dircmp}{a, b\optional{, ignore\optional{, hide}}}
60Construct a new directory comparison object, to compare the
61directories \var{a} and \var{b}. \var{ignore} is a list of names to
62ignore, and defaults to \code{['RCS', 'CVS', 'tags']}. \var{hide} is a
Skip Montanaro9483bed2001-01-18 10:44:08 +000063list of names to hide, and defaults to \code{[os.curdir, os.pardir]}.
Fred Drake353aaad2000-07-03 08:24:49 +000064\end{classdesc}
65
Fred Drakeba7e2c82001-05-11 17:01:32 +000066The \class{dircmp} class provides the following methods:
67
Fred Drake353aaad2000-07-03 08:24:49 +000068\begin{methoddesc}[dircmp]{report}{}
69Print (to \code{sys.stdout}) a comparison between \var{a} and \var{b}.
70\end{methoddesc}
71
72\begin{methoddesc}[dircmp]{report_partial_closure}{}
73Print a comparison between \var{a} and \var{b} and common immediate
74subdirctories.
75\end{methoddesc}
76
77\begin{methoddesc}[dircmp]{report_full_closure}{}
78Print a comparison between \var{a} and \var{b} and common
79subdirctories (recursively).
80\end{methoddesc}
81
Fred Drakeba7e2c82001-05-11 17:01:32 +000082
83The \class{dircmp} offers a number of interesting attributes that may
84be used to get various bits of information about the directory trees
85being compared.
86
87Note that via \method{__getattr__()} hooks, all attributes are
88computed lazilly, so there is no speed penalty if only those
89attributes which are lightweight to compute are used.
90
Fred Drake353aaad2000-07-03 08:24:49 +000091\begin{memberdesc}[dircmp]{left_list}
92Files and subdirectories in \var{a}, filtered by \var{hide} and
93\var{ignore}.
94\end{memberdesc}
95
96\begin{memberdesc}[dircmp]{right_list}
97Files and subdirectories in \var{b}, filtered by \var{hide} and
98\var{ignore}.
99\end{memberdesc}
100
101\begin{memberdesc}[dircmp]{common}
102Files and subdirectories in both \var{a} and \var{b}.
103\end{memberdesc}
104
105\begin{memberdesc}[dircmp]{left_only}
106Files and subdirectories only in \var{a}.
107\end{memberdesc}
108
109\begin{memberdesc}[dircmp]{right_only}
110Files and subdirectories only in \var{b}.
111\end{memberdesc}
112
113\begin{memberdesc}[dircmp]{common_dirs}
114Subdirectories in both \var{a} and \var{b}.
115\end{memberdesc}
116
117\begin{memberdesc}[dircmp]{common_files}
118Files in both \var{a} and \var{b}
119\end{memberdesc}
120
121\begin{memberdesc}[dircmp]{common_funny}
122Names in both \var{a} and \var{b}, such that the type differs between
123the directories, or names for which \function{os.stat()} reports an
124error.
125\end{memberdesc}
126
127\begin{memberdesc}[dircmp]{same_files}
128Files which are identical in both \var{a} and \var{b}.
129\end{memberdesc}
130
131\begin{memberdesc}[dircmp]{diff_files}
132Files which are in both \var{a} and \var{b}, whose contents differ.
133\end{memberdesc}
134
135\begin{memberdesc}[dircmp]{funny_files}
136Files which are in both \var{a} and \var{b}, but could not be
137compared.
138\end{memberdesc}
139
140\begin{memberdesc}[dircmp]{subdirs}
141A dictionary mapping names in \member{common_dirs} to
142\class{dircmp} objects.
143\end{memberdesc}