summaryrefslogtreecommitdiff
path: root/devdocs/python~3.12/library%2Ffilecmp.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/python~3.12/library%2Ffilecmp.html
new repository
Diffstat (limited to 'devdocs/python~3.12/library%2Ffilecmp.html')
-rw-r--r--devdocs/python~3.12/library%2Ffilecmp.html82
1 files changed, 82 insertions, 0 deletions
diff --git a/devdocs/python~3.12/library%2Ffilecmp.html b/devdocs/python~3.12/library%2Ffilecmp.html
new file mode 100644
index 00000000..a9af2ebd
--- /dev/null
+++ b/devdocs/python~3.12/library%2Ffilecmp.html
@@ -0,0 +1,82 @@
+ <span id="filecmp-file-and-directory-comparisons"></span><h1>filecmp — File and Directory Comparisons</h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/filecmp.py">Lib/filecmp.py</a></p> <p>The <a class="reference internal" href="#module-filecmp" title="filecmp: Compare files efficiently."><code>filecmp</code></a> module defines functions to compare files and directories, with various optional time/correctness trade-offs. For comparing files, see also the <a class="reference internal" href="difflib#module-difflib" title="difflib: Helpers for computing differences between objects."><code>difflib</code></a> module.</p> <p>The <a class="reference internal" href="#module-filecmp" title="filecmp: Compare files efficiently."><code>filecmp</code></a> module defines the following functions:</p> <dl class="py function"> <dt class="sig sig-object py" id="filecmp.cmp">
+<code>filecmp.cmp(f1, f2, shallow=True)</code> </dt> <dd>
+<p>Compare the files named <em>f1</em> and <em>f2</em>, returning <code>True</code> if they seem equal, <code>False</code> otherwise.</p> <p>If <em>shallow</em> is true and the <a class="reference internal" href="os#os.stat" title="os.stat"><code>os.stat()</code></a> signatures (file type, size, and modification time) of both files are identical, the files are taken to be equal.</p> <p>Otherwise, the files are treated as different if their sizes or contents differ.</p> <p>Note that no external programs are called from this function, giving it portability and efficiency.</p> <p>This function uses a cache for past comparisons and the results, with cache entries invalidated if the <a class="reference internal" href="os#os.stat" title="os.stat"><code>os.stat()</code></a> information for the file changes. The entire cache may be cleared using <a class="reference internal" href="#filecmp.clear_cache" title="filecmp.clear_cache"><code>clear_cache()</code></a>.</p> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="filecmp.cmpfiles">
+<code>filecmp.cmpfiles(dir1, dir2, common, shallow=True)</code> </dt> <dd>
+<p>Compare the files in the two directories <em>dir1</em> and <em>dir2</em> whose names are given by <em>common</em>.</p> <p>Returns three lists of file names: <em>match</em>, <em>mismatch</em>, <em>errors</em>. <em>match</em> contains the list of files that match, <em>mismatch</em> contains the names of those that don’t, and <em>errors</em> lists the names of files which could not be compared. Files are listed in <em>errors</em> if they don’t exist in one of the directories, the user lacks permission to read them or if the comparison could not be done for some other reason.</p> <p>The <em>shallow</em> parameter has the same meaning and default value as for <a class="reference internal" href="#filecmp.cmp" title="filecmp.cmp"><code>filecmp.cmp()</code></a>.</p> <p>For example, <code>cmpfiles('a', 'b', ['c', 'd/e'])</code> will compare <code>a/c</code> with <code>b/c</code> and <code>a/d/e</code> with <code>b/d/e</code>. <code>'c'</code> and <code>'d/e'</code> will each be in one of the three returned lists.</p> </dd>
+</dl> <dl class="py function"> <dt class="sig sig-object py" id="filecmp.clear_cache">
+<code>filecmp.clear_cache()</code> </dt> <dd>
+<p>Clear the filecmp cache. This may be useful if a file is compared so quickly after it is modified that it is within the mtime resolution of the underlying filesystem.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.4.</span></p> </div> </dd>
+</dl> <section id="the-dircmp-class"> <span id="dircmp-objects"></span><h2>The dircmp class</h2> <dl class="py class"> <dt class="sig sig-object py" id="filecmp.dircmp">
+<code>class filecmp.dircmp(a, b, ignore=None, hide=None)</code> </dt> <dd>
+<p>Construct a new directory comparison object, to compare the directories <em>a</em> and <em>b</em>. <em>ignore</em> is a list of names to ignore, and defaults to <a class="reference internal" href="#filecmp.DEFAULT_IGNORES" title="filecmp.DEFAULT_IGNORES"><code>filecmp.DEFAULT_IGNORES</code></a>. <em>hide</em> is a list of names to hide, and defaults to <code>[os.curdir, os.pardir]</code>.</p> <p>The <a class="reference internal" href="#filecmp.dircmp" title="filecmp.dircmp"><code>dircmp</code></a> class compares files by doing <em>shallow</em> comparisons as described for <a class="reference internal" href="#filecmp.cmp" title="filecmp.cmp"><code>filecmp.cmp()</code></a>.</p> <p>The <a class="reference internal" href="#filecmp.dircmp" title="filecmp.dircmp"><code>dircmp</code></a> class provides the following methods:</p> <dl class="py method"> <dt class="sig sig-object py" id="filecmp.dircmp.report">
+<code>report()</code> </dt> <dd>
+<p>Print (to <a class="reference internal" href="sys#sys.stdout" title="sys.stdout"><code>sys.stdout</code></a>) a comparison between <em>a</em> and <em>b</em>.</p> </dd>
+</dl> <dl class="py method"> <dt class="sig sig-object py" id="filecmp.dircmp.report_partial_closure">
+<code>report_partial_closure()</code> </dt> <dd>
+<p>Print a comparison between <em>a</em> and <em>b</em> and common immediate subdirectories.</p> </dd>
+</dl> <dl class="py method"> <dt class="sig sig-object py" id="filecmp.dircmp.report_full_closure">
+<code>report_full_closure()</code> </dt> <dd>
+<p>Print a comparison between <em>a</em> and <em>b</em> and common subdirectories (recursively).</p> </dd>
+</dl> <p>The <a class="reference internal" href="#filecmp.dircmp" title="filecmp.dircmp"><code>dircmp</code></a> class offers a number of interesting attributes that may be used to get various bits of information about the directory trees being compared.</p> <p>Note that via <a class="reference internal" href="../reference/datamodel#object.__getattr__" title="object.__getattr__"><code>__getattr__()</code></a> hooks, all attributes are computed lazily, so there is no speed penalty if only those attributes which are lightweight to compute are used.</p> <dl class="py attribute"> <dt class="sig sig-object py" id="filecmp.dircmp.left">
+<code>left</code> </dt> <dd>
+<p>The directory <em>a</em>.</p> </dd>
+</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="filecmp.dircmp.right">
+<code>right</code> </dt> <dd>
+<p>The directory <em>b</em>.</p> </dd>
+</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="filecmp.dircmp.left_list">
+<code>left_list</code> </dt> <dd>
+<p>Files and subdirectories in <em>a</em>, filtered by <em>hide</em> and <em>ignore</em>.</p> </dd>
+</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="filecmp.dircmp.right_list">
+<code>right_list</code> </dt> <dd>
+<p>Files and subdirectories in <em>b</em>, filtered by <em>hide</em> and <em>ignore</em>.</p> </dd>
+</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="filecmp.dircmp.common">
+<code>common</code> </dt> <dd>
+<p>Files and subdirectories in both <em>a</em> and <em>b</em>.</p> </dd>
+</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="filecmp.dircmp.left_only">
+<code>left_only</code> </dt> <dd>
+<p>Files and subdirectories only in <em>a</em>.</p> </dd>
+</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="filecmp.dircmp.right_only">
+<code>right_only</code> </dt> <dd>
+<p>Files and subdirectories only in <em>b</em>.</p> </dd>
+</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="filecmp.dircmp.common_dirs">
+<code>common_dirs</code> </dt> <dd>
+<p>Subdirectories in both <em>a</em> and <em>b</em>.</p> </dd>
+</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="filecmp.dircmp.common_files">
+<code>common_files</code> </dt> <dd>
+<p>Files in both <em>a</em> and <em>b</em>.</p> </dd>
+</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="filecmp.dircmp.common_funny">
+<code>common_funny</code> </dt> <dd>
+<p>Names in both <em>a</em> and <em>b</em>, such that the type differs between the directories, or names for which <a class="reference internal" href="os#os.stat" title="os.stat"><code>os.stat()</code></a> reports an error.</p> </dd>
+</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="filecmp.dircmp.same_files">
+<code>same_files</code> </dt> <dd>
+<p>Files which are identical in both <em>a</em> and <em>b</em>, using the class’s file comparison operator.</p> </dd>
+</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="filecmp.dircmp.diff_files">
+<code>diff_files</code> </dt> <dd>
+<p>Files which are in both <em>a</em> and <em>b</em>, whose contents differ according to the class’s file comparison operator.</p> </dd>
+</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="filecmp.dircmp.funny_files">
+<code>funny_files</code> </dt> <dd>
+<p>Files which are in both <em>a</em> and <em>b</em>, but could not be compared.</p> </dd>
+</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="filecmp.dircmp.subdirs">
+<code>subdirs</code> </dt> <dd>
+<p>A dictionary mapping names in <a class="reference internal" href="#filecmp.dircmp.common_dirs" title="filecmp.dircmp.common_dirs"><code>common_dirs</code></a> to <a class="reference internal" href="#filecmp.dircmp" title="filecmp.dircmp"><code>dircmp</code></a> instances (or MyDirCmp instances if this instance is of type MyDirCmp, a subclass of <a class="reference internal" href="#filecmp.dircmp" title="filecmp.dircmp"><code>dircmp</code></a>).</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.10: </span>Previously entries were always <a class="reference internal" href="#filecmp.dircmp" title="filecmp.dircmp"><code>dircmp</code></a> instances. Now entries are the same type as <em>self</em>, if <em>self</em> is a subclass of <a class="reference internal" href="#filecmp.dircmp" title="filecmp.dircmp"><code>dircmp</code></a>.</p> </div> </dd>
+</dl> </dd>
+</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="filecmp.DEFAULT_IGNORES">
+<code>filecmp.DEFAULT_IGNORES</code> </dt> <dd>
+<div class="versionadded"> <p><span class="versionmodified added">New in version 3.4.</span></p> </div> <p>List of directories ignored by <a class="reference internal" href="#filecmp.dircmp" title="filecmp.dircmp"><code>dircmp</code></a> by default.</p> </dd>
+</dl> <p>Here is a simplified example of using the <code>subdirs</code> attribute to search recursively through two directories to show common different files:</p> <pre data-language="python">&gt;&gt;&gt; from filecmp import dircmp
+&gt;&gt;&gt; def print_diff_files(dcmp):
+... for name in dcmp.diff_files:
+... print("diff_file %s found in %s and %s" % (name, dcmp.left,
+... dcmp.right))
+... for sub_dcmp in dcmp.subdirs.values():
+... print_diff_files(sub_dcmp)
+...
+&gt;&gt;&gt; dcmp = dircmp('dir1', 'dir2')
+&gt;&gt;&gt; print_diff_files(dcmp)
+</pre> </section> <div class="_attribution">
+ <p class="_attribution-p">
+ &copy; 2001&ndash;2023 Python Software Foundation<br>Licensed under the PSF License.<br>
+ <a href="https://docs.python.org/3.12/library/filecmp.html" class="_attribution-link">https://docs.python.org/3.12/library/filecmp.html</a>
+ </p>
+</div>