<!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 v2.6.6 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.6.6',
COLLAPSE_MODINDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="Search within Python v2.6.6 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 v2.6.6 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" />
</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="../modindex.html" title="Global Module Index"
accesskey="M">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="../index.html">Python v2.6.6 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">
<h1>10.5. <tt class="xref docutils literal"><span class="pre">filecmp</span></tt> — File and Directory Comparisons<a class="headerlink" href="#module-filecmp" title="Permalink to this headline">¶</a></h1>
<p>The <tt class="xref docutils literal"><span class="pre">filecmp</span></tt> module defines functions to compare files and directories,
with various optional time/correctness trade-offs. For comparing files,
see also the <a title="Helpers for computing differences between objects." class="reference external" href="difflib.html#module-difflib"><tt class="xref docutils literal"><span class="pre">difflib</span></tt></a> module.</p>
<p>The <tt class="xref docutils literal"><span class="pre">filecmp</span></tt> 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="xref docutils literal"><span class="pre">True</span></tt> if they seem equal,
<tt class="xref docutils literal"><span class="pre">False</span></tt> otherwise.</p>
<p>Unless <em>shallow</em> is given and is false, files with identical <a title="os.stat" class="reference external" href="os.html#os.stat"><tt class="xref 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 title="os.stat" class="reference external" href="os.html#os.stat"><tt class="xref 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 title="filecmp.cmp" class="reference internal" href="#filecmp.cmp"><tt class="xref 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 title="filecmp.dircmp" class="reference internal" href="#filecmp.dircmp"><tt class="xref 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 title="filecmp.dircmp" class="reference internal" href="#filecmp.dircmp"><tt class="xref 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 title="filecmp.dircmp" class="reference internal" href="#filecmp.dircmp"><tt class="xref 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>Print (to <tt class="docutils literal"><span class="pre">sys.stdout</span></tt>) a comparison between <em>a</em> and <em>b</em>.</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>Print a comparison between <em>a</em> and <em>b</em> and common immediate
subdirectories.</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>Print a comparison between <em>a</em> and <em>b</em> and common subdirectories
(recursively).</dd></dl>
<p>The <a title="filecmp.dircmp" class="reference internal" href="#filecmp.dircmp"><tt class="xref docutils literal"><span class="pre">dircmp</span></tt></a> 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 title="object.__getattr__" class="reference external" href="../reference/datamodel.html#object.__getattr__"><tt class="xref 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_list">
<tt class="descname">left_list</tt><a class="headerlink" href="#filecmp.dircmp.left_list" title="Permalink to this definition">¶</a></dt>
<dd>Files and subdirectories in <em>a</em>, filtered by <em>hide</em> and <em>ignore</em>.</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>Files and subdirectories in <em>b</em>, filtered by <em>hide</em> and <em>ignore</em>.</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>Files and subdirectories in both <em>a</em> and <em>b</em>.</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>Files and subdirectories only in <em>a</em>.</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>Files and subdirectories only in <em>b</em>.</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>Subdirectories in both <em>a</em> and <em>b</em>.</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>Files in both <em>a</em> and <em>b</em></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>Names in both <em>a</em> and <em>b</em>, such that the type differs between the
directories, or names for which <a title="os.stat" class="reference external" href="os.html#os.stat"><tt class="xref docutils literal"><span class="pre">os.stat()</span></tt></a> reports an error.</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>Files which are identical in both <em>a</em> and <em>b</em>.</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>Files which are in both <em>a</em> and <em>b</em>, whose contents differ.</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>Files which are in both <em>a</em> and <em>b</em>, but could not be compared.</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>A dictionary mapping names in <a title="filecmp.dircmp.common_dirs" class="reference internal" href="#filecmp.dircmp.common_dirs"><tt class="xref docutils literal"><span class="pre">common_dirs</span></tt></a> to <a title="filecmp.dircmp" class="reference internal" href="#filecmp.dircmp"><tt class="xref docutils literal"><span class="pre">dircmp</span></tt></a> objects.</dd></dl>
</dd></dl>
</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 external" href="#">10.5. <tt class="docutils literal"><span class="pre">filecmp</span></tt> — File and Directory Comparisons</a><ul>
<li><a class="reference external" 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" size="18" />
<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="../modindex.html" title="Global 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="../index.html">Python v2.6.6 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-2011, 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 20, 2011.
<a href="../bugs.html">Found a bug</a>?
<br />
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.6.
</div>
</body>
</html>
Copyright 2K16 - 2K18 Indonesian Hacker Rulez