| 1 | \section{\module{filecmp} ---
|
|---|
| 2 | File and Directory Comparisons}
|
|---|
| 3 |
|
|---|
| 4 | \declaremodule{standard}{filecmp}
|
|---|
| 5 | \sectionauthor{Moshe Zadka}{[email protected]}
|
|---|
| 6 | \modulesynopsis{Compare files efficiently.}
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 | The \module{filecmp} module defines functions to compare files and
|
|---|
| 10 | directories, with various optional time/correctness trade-offs.
|
|---|
| 11 |
|
|---|
| 12 | The \module{filecmp} module defines the following functions:
|
|---|
| 13 |
|
|---|
| 14 | \begin{funcdesc}{cmp}{f1, f2\optional{, shallow}}
|
|---|
| 15 | Compare the files named \var{f1} and \var{f2}, returning \code{True} if
|
|---|
| 16 | they seem equal, \code{False} otherwise.
|
|---|
| 17 |
|
|---|
| 18 | Unless \var{shallow} is given and is false, files with identical
|
|---|
| 19 | \function{os.stat()} signatures are taken to be equal.
|
|---|
| 20 |
|
|---|
| 21 | Files that were compared using this function will not be compared again
|
|---|
| 22 | unless their \function{os.stat()} signature changes.
|
|---|
| 23 |
|
|---|
| 24 | Note that no external programs are called from this function, giving it
|
|---|
| 25 | portability and efficiency.
|
|---|
| 26 | \end{funcdesc}
|
|---|
| 27 |
|
|---|
| 28 | \begin{funcdesc}{cmpfiles}{dir1, dir2, common\optional{,
|
|---|
| 29 | shallow}}
|
|---|
| 30 | Returns three lists of file names: \var{match}, \var{mismatch},
|
|---|
| 31 | \var{errors}. \var{match} contains the list of files match in both
|
|---|
| 32 | directories, \var{mismatch} includes the names of those that don't,
|
|---|
| 33 | and \var{errros} lists the names of files which could not be
|
|---|
| 34 | compared. Files may be listed in \var{errors} because the user may
|
|---|
| 35 | lack permission to read them or many other reasons, but always that
|
|---|
| 36 | the comparison could not be done for some reason.
|
|---|
| 37 |
|
|---|
| 38 | The \var{common} parameter is a list of file names found in both directories.
|
|---|
| 39 | The \var{shallow} parameter has the same
|
|---|
| 40 | meaning and default value as for \function{filecmp.cmp()}.
|
|---|
| 41 | \end{funcdesc}
|
|---|
| 42 |
|
|---|
| 43 | Example:
|
|---|
| 44 |
|
|---|
| 45 | \begin{verbatim}
|
|---|
| 46 | >>> import filecmp
|
|---|
| 47 | >>> filecmp.cmp('libundoc.tex', 'libundoc.tex')
|
|---|
| 48 | True
|
|---|
| 49 | >>> filecmp.cmp('libundoc.tex', 'lib.tex')
|
|---|
| 50 | False
|
|---|
| 51 | \end{verbatim}
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 | \subsection{The \protect\class{dircmp} class \label{dircmp-objects}}
|
|---|
| 55 |
|
|---|
| 56 | \class{dircmp} instances are built using this constructor:
|
|---|
| 57 |
|
|---|
| 58 | \begin{classdesc}{dircmp}{a, b\optional{, ignore\optional{, hide}}}
|
|---|
| 59 | Construct a new directory comparison object, to compare the
|
|---|
| 60 | directories \var{a} and \var{b}. \var{ignore} is a list of names to
|
|---|
| 61 | ignore, and defaults to \code{['RCS', 'CVS', 'tags']}. \var{hide} is a
|
|---|
| 62 | list of names to hide, and defaults to \code{[os.curdir, os.pardir]}.
|
|---|
| 63 | \end{classdesc}
|
|---|
| 64 |
|
|---|
| 65 | The \class{dircmp} class provides the following methods:
|
|---|
| 66 |
|
|---|
| 67 | \begin{methoddesc}[dircmp]{report}{}
|
|---|
| 68 | Print (to \code{sys.stdout}) a comparison between \var{a} and \var{b}.
|
|---|
| 69 | \end{methoddesc}
|
|---|
| 70 |
|
|---|
| 71 | \begin{methoddesc}[dircmp]{report_partial_closure}{}
|
|---|
| 72 | Print a comparison between \var{a} and \var{b} and common immediate
|
|---|
| 73 | subdirectories.
|
|---|
| 74 | \end{methoddesc}
|
|---|
| 75 |
|
|---|
| 76 | \begin{methoddesc}[dircmp]{report_full_closure}{}
|
|---|
| 77 | Print a comparison between \var{a} and \var{b} and common
|
|---|
| 78 | subdirectories (recursively).
|
|---|
| 79 | \end{methoddesc}
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 | The \class{dircmp} offers a number of interesting attributes that may
|
|---|
| 83 | be used to get various bits of information about the directory trees
|
|---|
| 84 | being compared.
|
|---|
| 85 |
|
|---|
| 86 | Note that via \method{__getattr__()} hooks, all attributes are
|
|---|
| 87 | computed lazily, so there is no speed penalty if only those
|
|---|
| 88 | attributes which are lightweight to compute are used.
|
|---|
| 89 |
|
|---|
| 90 | \begin{memberdesc}[dircmp]{left_list}
|
|---|
| 91 | Files and subdirectories in \var{a}, filtered by \var{hide} and
|
|---|
| 92 | \var{ignore}.
|
|---|
| 93 | \end{memberdesc}
|
|---|
| 94 |
|
|---|
| 95 | \begin{memberdesc}[dircmp]{right_list}
|
|---|
| 96 | Files and subdirectories in \var{b}, filtered by \var{hide} and
|
|---|
| 97 | \var{ignore}.
|
|---|
| 98 | \end{memberdesc}
|
|---|
| 99 |
|
|---|
| 100 | \begin{memberdesc}[dircmp]{common}
|
|---|
| 101 | Files and subdirectories in both \var{a} and \var{b}.
|
|---|
| 102 | \end{memberdesc}
|
|---|
| 103 |
|
|---|
| 104 | \begin{memberdesc}[dircmp]{left_only}
|
|---|
| 105 | Files and subdirectories only in \var{a}.
|
|---|
| 106 | \end{memberdesc}
|
|---|
| 107 |
|
|---|
| 108 | \begin{memberdesc}[dircmp]{right_only}
|
|---|
| 109 | Files and subdirectories only in \var{b}.
|
|---|
| 110 | \end{memberdesc}
|
|---|
| 111 |
|
|---|
| 112 | \begin{memberdesc}[dircmp]{common_dirs}
|
|---|
| 113 | Subdirectories in both \var{a} and \var{b}.
|
|---|
| 114 | \end{memberdesc}
|
|---|
| 115 |
|
|---|
| 116 | \begin{memberdesc}[dircmp]{common_files}
|
|---|
| 117 | Files in both \var{a} and \var{b}
|
|---|
| 118 | \end{memberdesc}
|
|---|
|
|---|