python-project/python-3.7.4-docs-html/library/filecmp.html

392 lines
25 KiB
HTML

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>filecmp — File and Directory Comparisons &#8212; Python 3.7.4 documentation</title>
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="../_static/language_data.js"></script>
<script type="text/javascript" src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="Search within Python 3.7.4 documentation"
href="../_static/opensearch.xml"/>
<link rel="author" title="About these documents" href="../about.html" />
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="copyright" title="Copyright" href="../copyright.html" />
<link rel="next" title="tempfile — Generate temporary files and directories" href="tempfile.html" />
<link rel="prev" title="stat — Interpreting stat() results" href="stat.html" />
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
<link rel="canonical" href="https://docs.python.org/3/library/filecmp.html" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/switchers.js"></script>
<style>
@media only screen {
table.full-width-table {
width: 100%;
}
}
</style>
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="tempfile.html" title="tempfile — Generate temporary files and directories"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="stat.html" title="stat — Interpreting stat() results"
accesskey="P">previous</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li>
<span class="language_switcher_placeholder">en</span>
<span class="version_switcher_placeholder">3.7.4</span>
<a href="../index.html">Documentation </a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="filesys.html" accesskey="U">File and Directory Access</a> &#187;</li>
<li class="right">
<div class="inline-search" style="display: none" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('.inline-search').show(0);</script>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="module-filecmp">
<span id="filecmp-file-and-directory-comparisons"></span><h1><a class="reference internal" href="#module-filecmp" title="filecmp: Compare files efficiently."><code class="xref py py-mod docutils literal notranslate"><span class="pre">filecmp</span></code></a> — File and Directory Comparisons<a class="headerlink" href="#module-filecmp" title="Permalink to this headline"></a></h1>
<p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.7/Lib/filecmp.py">Lib/filecmp.py</a></p>
<hr class="docutils" />
<p>The <a class="reference internal" href="#module-filecmp" title="filecmp: Compare files efficiently."><code class="xref py py-mod docutils literal notranslate"><span class="pre">filecmp</span></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.html#module-difflib" title="difflib: Helpers for computing differences between objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">difflib</span></code></a> module.</p>
<p>The <a class="reference internal" href="#module-filecmp" title="filecmp: Compare files efficiently."><code class="xref py py-mod docutils literal notranslate"><span class="pre">filecmp</span></code></a> module defines the following functions:</p>
<dl class="function">
<dt id="filecmp.cmp">
<code class="descclassname">filecmp.</code><code class="descname">cmp</code><span class="sig-paren">(</span><em>f1</em>, <em>f2</em>, <em>shallow=True</em><span class="sig-paren">)</span><a class="headerlink" href="#filecmp.cmp" title="Permalink to this definition"></a></dt>
<dd><p>Compare the files named <em>f1</em> and <em>f2</em>, returning <code class="docutils literal notranslate"><span class="pre">True</span></code> if they seem equal,
<code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>
<p>If <em>shallow</em> is true, files with identical <a class="reference internal" href="os.html#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.stat()</span></code></a> signatures are
taken to be equal. Otherwise, the contents of the files are compared.</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.html#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.stat()</span></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 class="xref py py-func docutils literal notranslate"><span class="pre">clear_cache()</span></code></a>.</p>
</dd></dl>
<dl class="function">
<dt id="filecmp.cmpfiles">
<code class="descclassname">filecmp.</code><code class="descname">cmpfiles</code><span class="sig-paren">(</span><em>dir1</em>, <em>dir2</em>, <em>common</em>, <em>shallow=True</em><span class="sig-paren">)</span><a class="headerlink" href="#filecmp.cmpfiles" title="Permalink to this definition"></a></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 dont, and <em>errors</em> lists the names of files which
could not be compared. Files are listed in <em>errors</em> if they dont 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 class="xref py py-func docutils literal notranslate"><span class="pre">filecmp.cmp()</span></code></a>.</p>
<p>For example, <code class="docutils literal notranslate"><span class="pre">cmpfiles('a',</span> <span class="pre">'b',</span> <span class="pre">['c',</span> <span class="pre">'d/e'])</span></code> will compare <code class="docutils literal notranslate"><span class="pre">a/c</span></code> with
<code class="docutils literal notranslate"><span class="pre">b/c</span></code> and <code class="docutils literal notranslate"><span class="pre">a/d/e</span></code> with <code class="docutils literal notranslate"><span class="pre">b/d/e</span></code>. <code class="docutils literal notranslate"><span class="pre">'c'</span></code> and <code class="docutils literal notranslate"><span class="pre">'d/e'</span></code> will each be in
one of the three returned lists.</p>
</dd></dl>
<dl class="function">
<dt id="filecmp.clear_cache">
<code class="descclassname">filecmp.</code><code class="descname">clear_cache</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#filecmp.clear_cache" title="Permalink to this definition"></a></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>
<div class="section" id="the-dircmp-class">
<span id="dircmp-objects"></span><h2>The <a class="reference internal" href="#filecmp.dircmp" title="filecmp.dircmp"><code class="xref py py-class docutils literal notranslate"><span class="pre">dircmp</span></code></a> class<a class="headerlink" href="#the-dircmp-class" title="Permalink to this headline"></a></h2>
<dl class="class">
<dt id="filecmp.dircmp">
<em class="property">class </em><code class="descclassname">filecmp.</code><code class="descname">dircmp</code><span class="sig-paren">(</span><em>a</em>, <em>b</em>, <em>ignore=None</em>, <em>hide=None</em><span class="sig-paren">)</span><a class="headerlink" href="#filecmp.dircmp" title="Permalink to this definition"></a></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 class="xref py py-attr docutils literal notranslate"><span class="pre">filecmp.DEFAULT_IGNORES</span></code></a>. <em>hide</em> is a list of names to hide, and
defaults to <code class="docutils literal notranslate"><span class="pre">[os.curdir,</span> <span class="pre">os.pardir]</span></code>.</p>
<p>The <a class="reference internal" href="#filecmp.dircmp" title="filecmp.dircmp"><code class="xref py py-class docutils literal notranslate"><span class="pre">dircmp</span></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 class="xref py py-func docutils literal notranslate"><span class="pre">filecmp.cmp()</span></code></a>.</p>
<p>The <a class="reference internal" href="#filecmp.dircmp" title="filecmp.dircmp"><code class="xref py py-class docutils literal notranslate"><span class="pre">dircmp</span></code></a> class provides the following methods:</p>
<dl class="method">
<dt id="filecmp.dircmp.report">
<code class="descname">report</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#filecmp.dircmp.report" title="Permalink to this definition"></a></dt>
<dd><p>Print (to <a class="reference internal" href="sys.html#sys.stdout" title="sys.stdout"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdout</span></code></a>) a comparison between <em>a</em> and <em>b</em>.</p>
</dd></dl>
<dl class="method">
<dt id="filecmp.dircmp.report_partial_closure">
<code class="descname">report_partial_closure</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#filecmp.dircmp.report_partial_closure" title="Permalink to this definition"></a></dt>
<dd><p>Print a comparison between <em>a</em> and <em>b</em> and common immediate
subdirectories.</p>
</dd></dl>
<dl class="method">
<dt id="filecmp.dircmp.report_full_closure">
<code class="descname">report_full_closure</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#filecmp.dircmp.report_full_closure" title="Permalink to this definition"></a></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 class="xref py py-class docutils literal notranslate"><span class="pre">dircmp</span></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.html#object.__getattr__" title="object.__getattr__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getattr__()</span></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="attribute">
<dt id="filecmp.dircmp.left">
<code class="descname">left</code><a class="headerlink" href="#filecmp.dircmp.left" title="Permalink to this definition"></a></dt>
<dd><p>The directory <em>a</em>.</p>
</dd></dl>
<dl class="attribute">
<dt id="filecmp.dircmp.right">
<code class="descname">right</code><a class="headerlink" href="#filecmp.dircmp.right" title="Permalink to this definition"></a></dt>
<dd><p>The directory <em>b</em>.</p>
</dd></dl>
<dl class="attribute">
<dt id="filecmp.dircmp.left_list">
<code class="descname">left_list</code><a class="headerlink" href="#filecmp.dircmp.left_list" title="Permalink to this definition"></a></dt>
<dd><p>Files and subdirectories in <em>a</em>, filtered by <em>hide</em> and <em>ignore</em>.</p>
</dd></dl>
<dl class="attribute">
<dt id="filecmp.dircmp.right_list">
<code class="descname">right_list</code><a class="headerlink" href="#filecmp.dircmp.right_list" title="Permalink to this definition"></a></dt>
<dd><p>Files and subdirectories in <em>b</em>, filtered by <em>hide</em> and <em>ignore</em>.</p>
</dd></dl>
<dl class="attribute">
<dt id="filecmp.dircmp.common">
<code class="descname">common</code><a class="headerlink" href="#filecmp.dircmp.common" title="Permalink to this definition"></a></dt>
<dd><p>Files and subdirectories in both <em>a</em> and <em>b</em>.</p>
</dd></dl>
<dl class="attribute">
<dt id="filecmp.dircmp.left_only">
<code class="descname">left_only</code><a class="headerlink" href="#filecmp.dircmp.left_only" title="Permalink to this definition"></a></dt>
<dd><p>Files and subdirectories only in <em>a</em>.</p>
</dd></dl>
<dl class="attribute">
<dt id="filecmp.dircmp.right_only">
<code class="descname">right_only</code><a class="headerlink" href="#filecmp.dircmp.right_only" title="Permalink to this definition"></a></dt>
<dd><p>Files and subdirectories only in <em>b</em>.</p>
</dd></dl>
<dl class="attribute">
<dt id="filecmp.dircmp.common_dirs">
<code class="descname">common_dirs</code><a class="headerlink" href="#filecmp.dircmp.common_dirs" title="Permalink to this definition"></a></dt>
<dd><p>Subdirectories in both <em>a</em> and <em>b</em>.</p>
</dd></dl>
<dl class="attribute">
<dt id="filecmp.dircmp.common_files">
<code class="descname">common_files</code><a class="headerlink" href="#filecmp.dircmp.common_files" title="Permalink to this definition"></a></dt>
<dd><p>Files in both <em>a</em> and <em>b</em>.</p>
</dd></dl>
<dl class="attribute">
<dt id="filecmp.dircmp.common_funny">
<code class="descname">common_funny</code><a class="headerlink" href="#filecmp.dircmp.common_funny" title="Permalink to this definition"></a></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.html#os.stat" title="os.stat"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.stat()</span></code></a> reports an error.</p>
</dd></dl>
<dl class="attribute">
<dt id="filecmp.dircmp.same_files">
<code class="descname">same_files</code><a class="headerlink" href="#filecmp.dircmp.same_files" title="Permalink to this definition"></a></dt>
<dd><p>Files which are identical in both <em>a</em> and <em>b</em>, using the classs
file comparison operator.</p>
</dd></dl>
<dl class="attribute">
<dt id="filecmp.dircmp.diff_files">
<code class="descname">diff_files</code><a class="headerlink" href="#filecmp.dircmp.diff_files" title="Permalink to this definition"></a></dt>
<dd><p>Files which are in both <em>a</em> and <em>b</em>, whose contents differ according
to the classs file comparison operator.</p>
</dd></dl>
<dl class="attribute">
<dt id="filecmp.dircmp.funny_files">
<code class="descname">funny_files</code><a class="headerlink" href="#filecmp.dircmp.funny_files" title="Permalink to this definition"></a></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="attribute">
<dt id="filecmp.dircmp.subdirs">
<code class="descname">subdirs</code><a class="headerlink" href="#filecmp.dircmp.subdirs" title="Permalink to this definition"></a></dt>
<dd><p>A dictionary mapping names in <a class="reference internal" href="#filecmp.dircmp.common_dirs" title="filecmp.dircmp.common_dirs"><code class="xref py py-attr docutils literal notranslate"><span class="pre">common_dirs</span></code></a> to <a class="reference internal" href="#filecmp.dircmp" title="filecmp.dircmp"><code class="xref py py-class docutils literal notranslate"><span class="pre">dircmp</span></code></a>
objects.</p>
</dd></dl>
</dd></dl>
<dl class="attribute">
<dt id="filecmp.DEFAULT_IGNORES">
<code class="descclassname">filecmp.</code><code class="descname">DEFAULT_IGNORES</code><a class="headerlink" href="#filecmp.DEFAULT_IGNORES" title="Permalink to this definition"></a></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 class="xref py py-class docutils literal notranslate"><span class="pre">dircmp</span></code></a> by default.</p>
</dd></dl>
<p>Here is a simplified example of using the <code class="docutils literal notranslate"><span class="pre">subdirs</span></code> attribute to search
recursively through two directories to show common different files:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">filecmp</span> <span class="k">import</span> <span class="n">dircmp</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">print_diff_files</span><span class="p">(</span><span class="n">dcmp</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">dcmp</span><span class="o">.</span><span class="n">diff_files</span><span class="p">:</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;diff_file </span><span class="si">%s</span><span class="s2"> found in </span><span class="si">%s</span><span class="s2"> and </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">dcmp</span><span class="o">.</span><span class="n">left</span><span class="p">,</span>
<span class="gp">... </span> <span class="n">dcmp</span><span class="o">.</span><span class="n">right</span><span class="p">))</span>
<span class="gp">... </span> <span class="k">for</span> <span class="n">sub_dcmp</span> <span class="ow">in</span> <span class="n">dcmp</span><span class="o">.</span><span class="n">subdirs</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
<span class="gp">... </span> <span class="n">print_diff_files</span><span class="p">(</span><span class="n">sub_dcmp</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dcmp</span> <span class="o">=</span> <span class="n">dircmp</span><span class="p">(</span><span class="s1">&#39;dir1&#39;</span><span class="p">,</span> <span class="s1">&#39;dir2&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">print_diff_files</span><span class="p">(</span><span class="n">dcmp</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../contents.html">Table of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">filecmp</span></code> — File and Directory Comparisons</a><ul>
<li><a class="reference internal" href="#the-dircmp-class">The <code class="xref py py-class docutils literal notranslate"><span class="pre">dircmp</span></code> class</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="stat.html"
title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">stat</span></code> — Interpreting <code class="xref py py-func docutils literal notranslate"><span class="pre">stat()</span></code> results</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="tempfile.html"
title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tempfile</span></code> — Generate temporary files and directories</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">Report a Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.7/Doc/library/filecmp.rst"
rel="nofollow">Show Source
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="tempfile.html" title="tempfile — Generate temporary files and directories"
>next</a> |</li>
<li class="right" >
<a href="stat.html" title="stat — Interpreting stat() results"
>previous</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li>
<span class="language_switcher_placeholder">en</span>
<span class="version_switcher_placeholder">3.7.4</span>
<a href="../index.html">Documentation </a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="filesys.html" >File and Directory Access</a> &#187;</li>
<li class="right">
<div class="inline-search" style="display: none" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('.inline-search').show(0);</script>
|
</li>
</ul>
</div>
<div class="footer">
&copy; <a href="../copyright.html">Copyright</a> 2001-2019, Python Software Foundation.
<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
Last updated on Jul 13, 2019.
<a href="../bugs.html">Found a bug</a>?
<br />
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.0.1.
</div>
</body>
</html>