korsygfhrtzangaiide
Elepffwdsff
/
usr
/
share
/
doc
/
python-docs-2.7.5
/
html
/
library
/
Upload FileeE
HOME
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>10.5. filecmp — File and Directory Comparisons — Python 2.7.5 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '2.7.5', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </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/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python 2.7.5 documentation" href="../index.html" /> <link rel="up" title="10. File and Directory Access" href="filesys.html" /> <link rel="next" title="10.6. tempfile — Generate temporary files and directories" href="tempfile.html" /> <link rel="prev" title="10.4. statvfs — Constants used with os.statvfs()" href="statvfs.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head> <body> <div class="related"> <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="10.6. tempfile — Generate temporary files and directories" accesskey="N">next</a> |</li> <li class="right" > <a href="statvfs.html" title="10.4. statvfs — Constants used with os.statvfs()" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="filesys.html" accesskey="U">10. File and Directory Access</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="module-filecmp"> <span id="filecmp-file-and-directory-comparisons"></span><h1>10.5. <a class="reference internal" href="#module-filecmp" title="filecmp: Compare files efficiently."><tt class="xref py py-mod docutils literal"><span class="pre">filecmp</span></tt></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="http://hg.python.org/cpython/file/2.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."><tt class="xref py py-mod docutils literal"><span class="pre">filecmp</span></tt></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."><tt class="xref py py-mod docutils literal"><span class="pre">difflib</span></tt></a> module.</p> <p>The <a class="reference internal" href="#module-filecmp" title="filecmp: Compare files efficiently."><tt class="xref py py-mod docutils literal"><span class="pre">filecmp</span></tt></a> module defines the following functions:</p> <dl class="function"> <dt id="filecmp.cmp"> <tt class="descclassname">filecmp.</tt><tt class="descname">cmp</tt><big>(</big><em>f1</em>, <em>f2</em><span class="optional">[</span>, <em>shallow</em><span class="optional">]</span><big>)</big><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 <tt class="docutils literal"><span class="pre">True</span></tt> if they seem equal, <tt class="docutils literal"><span class="pre">False</span></tt> otherwise.</p> <p>Unless <em>shallow</em> is given and is false, files with identical <a class="reference internal" href="os.html#os.stat" title="os.stat"><tt class="xref py py-func docutils literal"><span class="pre">os.stat()</span></tt></a> signatures are taken to be equal.</p> <p>Files that were compared using this function will not be compared again unless their <a class="reference internal" href="os.html#os.stat" title="os.stat"><tt class="xref py py-func docutils literal"><span class="pre">os.stat()</span></tt></a> signature changes.</p> <p>Note that no external programs are called from this function, giving it portability and efficiency.</p> </dd></dl> <dl class="function"> <dt id="filecmp.cmpfiles"> <tt class="descclassname">filecmp.</tt><tt class="descname">cmpfiles</tt><big>(</big><em>dir1</em>, <em>dir2</em>, <em>common</em><span class="optional">[</span>, <em>shallow</em><span class="optional">]</span><big>)</big><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 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"><tt class="xref py py-func docutils literal"><span class="pre">filecmp.cmp()</span></tt></a>.</p> <p>For example, <tt class="docutils literal"><span class="pre">cmpfiles('a',</span> <span class="pre">'b',</span> <span class="pre">['c',</span> <span class="pre">'d/e'])</span></tt> will compare <tt class="docutils literal"><span class="pre">a/c</span></tt> with <tt class="docutils literal"><span class="pre">b/c</span></tt> and <tt class="docutils literal"><span class="pre">a/d/e</span></tt> with <tt class="docutils literal"><span class="pre">b/d/e</span></tt>. <tt class="docutils literal"><span class="pre">'c'</span></tt> and <tt class="docutils literal"><span class="pre">'d/e'</span></tt> will each be in one of the three returned lists.</p> </dd></dl> <p>Example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">filecmp</span> <span class="gp">>>> </span><span class="n">filecmp</span><span class="o">.</span><span class="n">cmp</span><span class="p">(</span><span class="s">'undoc.rst'</span><span class="p">,</span> <span class="s">'undoc.rst'</span><span class="p">)</span> <span class="go">True</span> <span class="gp">>>> </span><span class="n">filecmp</span><span class="o">.</span><span class="n">cmp</span><span class="p">(</span><span class="s">'undoc.rst'</span><span class="p">,</span> <span class="s">'index.rst'</span><span class="p">)</span> <span class="go">False</span> </pre></div> </div> <div class="section" id="the-dircmp-class"> <span id="dircmp-objects"></span><h2>10.5.1. The <a class="reference internal" href="#filecmp.dircmp" title="filecmp.dircmp"><tt class="xref py py-class docutils literal"><span class="pre">dircmp</span></tt></a> class<a class="headerlink" href="#the-dircmp-class" title="Permalink to this headline">¶</a></h2> <p><a class="reference internal" href="#filecmp.dircmp" title="filecmp.dircmp"><tt class="xref py py-class docutils literal"><span class="pre">dircmp</span></tt></a> instances are built using this constructor:</p> <dl class="class"> <dt id="filecmp.dircmp"> <em class="property">class </em><tt class="descclassname">filecmp.</tt><tt class="descname">dircmp</tt><big>(</big><em>a</em>, <em>b</em><span class="optional">[</span>, <em>ignore</em><span class="optional">[</span>, <em>hide</em><span class="optional">]</span><span class="optional">]</span><big>)</big><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 <tt class="docutils literal"><span class="pre">['RCS',</span> <span class="pre">'CVS',</span> <span class="pre">'tags']</span></tt>. <em>hide</em> is a list of names to hide, and defaults to <tt class="docutils literal"><span class="pre">[os.curdir,</span> <span class="pre">os.pardir]</span></tt>.</p> <p>The <a class="reference internal" href="#filecmp.dircmp" title="filecmp.dircmp"><tt class="xref py py-class docutils literal"><span class="pre">dircmp</span></tt></a> class compares files by doing <em>shallow</em> comparisons as described for <a class="reference internal" href="#filecmp.cmp" title="filecmp.cmp"><tt class="xref py py-func docutils literal"><span class="pre">filecmp.cmp()</span></tt></a>.</p> <p>The <a class="reference internal" href="#filecmp.dircmp" title="filecmp.dircmp"><tt class="xref py py-class docutils literal"><span class="pre">dircmp</span></tt></a> class provides the following methods:</p> <dl class="method"> <dt id="filecmp.dircmp.report"> <tt class="descname">report</tt><big>(</big><big>)</big><a class="headerlink" href="#filecmp.dircmp.report" title="Permalink to this definition">¶</a></dt> <dd><p>Print (to <tt class="docutils literal"><span class="pre">sys.stdout</span></tt>) a comparison between <em>a</em> and <em>b</em>.</p> </dd></dl> <dl class="method"> <dt id="filecmp.dircmp.report_partial_closure"> <tt class="descname">report_partial_closure</tt><big>(</big><big>)</big><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"> <tt class="descname">report_full_closure</tt><big>(</big><big>)</big><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"><tt class="xref py py-class docutils literal"><span class="pre">dircmp</span></tt></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__"><tt class="xref py py-meth docutils literal"><span class="pre">__getattr__()</span></tt></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"> <tt class="descname">left</tt><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"> <tt class="descname">right</tt><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"> <tt class="descname">left_list</tt><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"> <tt class="descname">right_list</tt><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"> <tt class="descname">common</tt><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"> <tt class="descname">left_only</tt><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"> <tt class="descname">right_only</tt><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"> <tt class="descname">common_dirs</tt><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"> <tt class="descname">common_files</tt><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"> <tt class="descname">common_funny</tt><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"><tt class="xref py py-func docutils literal"><span class="pre">os.stat()</span></tt></a> reports an error.</p> </dd></dl> <dl class="attribute"> <dt id="filecmp.dircmp.same_files"> <tt class="descname">same_files</tt><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 class’s file comparison operator.</p> </dd></dl> <dl class="attribute"> <dt id="filecmp.dircmp.diff_files"> <tt class="descname">diff_files</tt><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 class’s file comparison operator.</p> </dd></dl> <dl class="attribute"> <dt id="filecmp.dircmp.funny_files"> <tt class="descname">funny_files</tt><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"> <tt class="descname">subdirs</tt><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"><tt class="xref py py-attr docutils literal"><span class="pre">common_dirs</span></tt></a> to <a class="reference internal" href="#filecmp.dircmp" title="filecmp.dircmp"><tt class="xref py py-class docutils literal"><span class="pre">dircmp</span></tt></a> objects.</p> </dd></dl> </dd></dl> <p>Here is a simplified example of using the <tt class="docutils literal"><span class="pre">subdirs</span></tt> attribute to search recursively through two directories to show common different files:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">filecmp</span> <span class="kn">import</span> <span class="n">dircmp</span> <span class="gp">>>> </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="k">print</span> <span class="s">"diff_file </span><span class="si">%s</span><span class="s"> found in </span><span class="si">%s</span><span class="s"> and </span><span class="si">%s</span><span class="s">"</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">>>> </span><span class="n">dcmp</span> <span class="o">=</span> <span class="n">dircmp</span><span class="p">(</span><span class="s">'dir1'</span><span class="p">,</span> <span class="s">'dir2'</span><span class="p">)</span> <span class="gp">>>> </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"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">10.5. <tt class="docutils literal"><span class="pre">filecmp</span></tt> — File and Directory Comparisons</a><ul> <li><a class="reference internal" href="#the-dircmp-class">10.5.1. The <tt class="docutils literal"><span class="pre">dircmp</span></tt> class</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="statvfs.html" title="previous chapter">10.4. <tt class="docutils literal"><span class="pre">statvfs</span></tt> — Constants used with <tt class="docutils literal"><span class="pre">os.statvfs()</span></tt></a></p> <h4>Next topic</h4> <p class="topless"><a href="tempfile.html" title="next chapter">10.6. <tt class="docutils literal"><span class="pre">tempfile</span></tt> — Generate temporary files and directories</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/library/filecmp.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input 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> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <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="10.6. tempfile — Generate temporary files and directories" >next</a> |</li> <li class="right" > <a href="statvfs.html" title="10.4. statvfs — Constants used with os.statvfs()" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="filesys.html" >10. File and Directory Access</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2019, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Jul 03, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>