cinap_lenrekadd hg and python4bb9e2215 years agoHistory
142 lines · 4.5 KB · TeX
RawDownload
\section{\module{filecmp} ---         File and Directory Comparisons} \declaremodule{standard}{filecmp}\sectionauthor{Moshe Zadka}{moshez@zadka.site.co.il}\modulesynopsis{Compare files efficiently.}  The \module{filecmp} module defines functions to compare files anddirectories, with various optional time/correctness trade-offs. The \module{filecmp} module defines the following functions: \begin{funcdesc}{cmp}{f1, f2\optional{, shallow}}Compare the files named \var{f1} and \var{f2}, returning \code{True} ifthey seem equal, \code{False} otherwise. Unless \var{shallow} is given and is false, files with identical\function{os.stat()} signatures are taken to be equal. Files that were compared using this function will not be compared againunless their \function{os.stat()} signature changes. Note that no external programs are called from this function, giving itportability and efficiency.\end{funcdesc} \begin{funcdesc}{cmpfiles}{dir1, dir2, common\optional{,                           shallow}}Returns three lists of file names: \var{match}, \var{mismatch},\var{errors}.  \var{match} contains the list of files match in bothdirectories, \var{mismatch} includes the names of those that don't,and \var{errros} lists the names of files which could not becompared.  Files may be listed in \var{errors} because the user maylack permission to read them or many other reasons, but always thatthe comparison could not be done for some reason. The \var{common} parameter is a list of file names found in both directories.The \var{shallow} parameter has the samemeaning and default value as for \function{filecmp.cmp()}.\end{funcdesc} Example: \begin{verbatim}>>> import filecmp>>> filecmp.cmp('libundoc.tex', 'libundoc.tex')True>>> filecmp.cmp('libundoc.tex', 'lib.tex')False\end{verbatim}  \subsection{The \protect\class{dircmp} class \label{dircmp-objects}} \class{dircmp} instances are built using this constructor: \begin{classdesc}{dircmp}{a, b\optional{, ignore\optional{, hide}}}Construct a new directory comparison object, to compare thedirectories \var{a} and \var{b}. \var{ignore} is a list of names toignore, and defaults to \code{['RCS', 'CVS', 'tags']}. \var{hide} is alist of names to hide, and defaults to \code{[os.curdir, os.pardir]}.\end{classdesc} The \class{dircmp} class provides the following methods: \begin{methoddesc}[dircmp]{report}{}Print (to \code{sys.stdout}) a comparison between \var{a} and \var{b}.\end{methoddesc} \begin{methoddesc}[dircmp]{report_partial_closure}{}Print a comparison between \var{a} and \var{b} and common immediatesubdirectories.\end{methoddesc} \begin{methoddesc}[dircmp]{report_full_closure}{}Print a comparison between \var{a} and \var{b} and common subdirectories (recursively).\end{methoddesc}  The \class{dircmp} offers a number of interesting attributes that maybe used to get various bits of information about the directory treesbeing compared. Note that via \method{__getattr__()} hooks, all attributes arecomputed lazily, so there is no speed penalty if only thoseattributes which are lightweight to compute are used. \begin{memberdesc}[dircmp]{left_list}Files and subdirectories in \var{a}, filtered by \var{hide} and\var{ignore}.\end{memberdesc} \begin{memberdesc}[dircmp]{right_list}Files and subdirectories in \var{b}, filtered by \var{hide} and\var{ignore}.\end{memberdesc} \begin{memberdesc}[dircmp]{common}Files and subdirectories in both \var{a} and \var{b}.\end{memberdesc} \begin{memberdesc}[dircmp]{left_only}Files and subdirectories only in \var{a}.\end{memberdesc} \begin{memberdesc}[dircmp]{right_only}Files and subdirectories only in \var{b}.\end{memberdesc} \begin{memberdesc}[dircmp]{common_dirs}Subdirectories in both \var{a} and \var{b}.\end{memberdesc} \begin{memberdesc}[dircmp]{common_files}Files in both \var{a} and \var{b}\end{memberdesc} \begin{memberdesc}[dircmp]{common_funny}Names in both \var{a} and \var{b}, such that the type differs betweenthe directories, or names for which \function{os.stat()} reports anerror.\end{memberdesc} \begin{memberdesc}[dircmp]{same_files}Files which are identical in both \var{a} and \var{b}.\end{memberdesc} \begin{memberdesc}[dircmp]{diff_files}Files which are in both \var{a} and \var{b}, whose contents differ.\end{memberdesc} \begin{memberdesc}[dircmp]{funny_files}Files which are in both \var{a} and \var{b}, but could not becompared.\end{memberdesc} \begin{memberdesc}[dircmp]{subdirs}A dictionary mapping names in \member{common_dirs} to\class{dircmp} objects.\end{memberdesc}