MOON
Server: Apache/2.2.31 (Unix) mod_ssl/2.2.31 OpenSSL/0.9.8e-fips-rhel5 mod_bwlimited/1.4
System: Linux csr818.wilogic.com 2.6.18-419.el5xen #1 SMP Fri Feb 24 22:50:37 UTC 2017 x86_64
User: digitals (531)
PHP: 5.4.45
Disabled: NONE
Upload Files
File: //usr/share/doc/python-docs-2.4.3/html/lib/module-tarfile.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="lib.html" title='Python Library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-readline.html" />
<link rel="prev" href="module-zipfile.html" />
<link rel="parent" href="someos.html" />
<link rel="next" href="tarfile-objects.html" />
<meta name='aesop' content='information' />
<title>7.19 tarfile -- Read and write tar archive files</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="7.18.3 ZipInfo Objects"
  href="zipinfo-objects.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="7. Optional Operating System"
  href="someos.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="7.19.1 TarFile Objects"
  href="tarfile-objects.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="zipinfo-objects.html">7.18.3 ZipInfo Objects</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="someos.html">7. Optional Operating System</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="tarfile-objects.html">7.19.1 TarFile Objects</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION0091900000000000000000">
7.19 <tt class="module">tarfile</tt> -- Read and write tar archive files</A>
</H1>

<P>
<A NAME="module-tarfile"></A>

<span class="versionnote">New in version 2.3.</span>

<P>

<P>
The <tt class="module">tarfile</tt> module makes it possible to read and create tar archives.
Some facts and figures:

<P>

<UL>
<LI>reads and writes <tt class="module">gzip</tt> and <tt class="module">bzip2</tt> compressed archives.
</LI>
<LI>creates POSIX 1003.1-1990 compliant or GNU tar compatible archives.
</LI>
<LI>reads GNU tar extensions <em>longname</em>, <em>longlink</em> and
      <em>sparse</em>.
</LI>
<LI>stores pathnames of unlimited length using GNU tar extensions.
</LI>
<LI>handles directories, regular files, hardlinks, symbolic links, fifos,
      character devices and block devices and is able to acquire and
      restore file information like timestamp, access permissions and owner.
</LI>
<LI>can handle tape devices.
</LI>
</UL>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2889' xml:id='l2h-2889' class="function">open</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>name</var><big>[</big><var>, mode
                       </var><big>[</big><var>, fileobj</var><big>[</big><var>, bufsize</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
    Return a <tt class="class">TarFile</tt> object for the pathname <var>name</var>.
    For detailed information on <tt class="class">TarFile</tt> objects,
    see <em class="citetitle"
 >TarFile Objects</em> (section <A href="tarfile-objects.html#tarfile-objects">7.19.1</A>).

<P>
<var>mode</var> has to be a string of the form <code>'filemode[:compression]'</code>,
    it defaults to <code>'r'</code>. Here is a full list of mode combinations:

<P>
<div class="center"><table class="realtable">
  <thead>
    <tr>
      <th class="center">mode</th>
      <th class="left"  >action</th>
      </tr>
    </thead>
  <tbody>
    <tr><td class="center" valign="baseline"><code>'r'</code></td>
        <td class="left"  >Open for reading with transparent compression (recommended).</td></tr>
    <tr><td class="center" valign="baseline"><code>'r:'</code></td>
        <td class="left"  >Open for reading exclusively without compression.</td></tr>
    <tr><td class="center" valign="baseline"><code>'r:gz'</code></td>
        <td class="left"  >Open for reading with gzip compression.</td></tr>
    <tr><td class="center" valign="baseline"><code>'r:bz2'</code></td>
        <td class="left"  >Open for reading with bzip2 compression.</td></tr>
    <tr><td class="center" valign="baseline"><code>'a' or 'a:'</code></td>
        <td class="left"  >Open for appending with no compression.</td></tr>
    <tr><td class="center" valign="baseline"><code>'w' or 'w:'</code></td>
        <td class="left"  >Open for uncompressed writing.</td></tr>
    <tr><td class="center" valign="baseline"><code>'w:gz'</code></td>
        <td class="left"  >Open for gzip compressed writing.</td></tr>
    <tr><td class="center" valign="baseline"><code>'w:bz2'</code></td>
        <td class="left"  >Open for bzip2 compressed writing.</td></tr></tbody>
</table></div>

<P>
Note that <code>'a:gz'</code> or <code>'a:bz2'</code> is not possible.
    If <var>mode</var> is not suitable to open a certain (compressed) file for
    reading, <tt class="exception">ReadError</tt> is raised. Use <var>mode</var> <code>'r'</code> to
    avoid this.  If a compression method is not supported,
    <tt class="exception">CompressionError</tt> is raised.

<P>
If <var>fileobj</var> is specified, it is used as an alternative to
    a file object opened for <var>name</var>.

<P>
For special purposes, there is a second format for <var>mode</var>:
    <code>'filemode|[compression]'</code>.  <tt class="function">open()</tt> will return a
    <tt class="class">TarFile</tt> object that processes its data as a stream of
    blocks.  No random seeking will be done on the file. If given,
    <var>fileobj</var> may be any object that has a <tt class="method">read()</tt> or
    <tt class="method">write()</tt> method (depending on the <var>mode</var>).
    <var>bufsize</var> specifies the blocksize and defaults to <code>20 *
    512</code> bytes. Use this variant in combination with
    e.g. <code>sys.stdin</code>, a socket file object or a tape device.
    However, such a <tt class="class">TarFile</tt> object is limited in that it does
    not allow to be accessed randomly, see ``Examples''
    (section&nbsp;<A href="tar-examples.html#tar-examples">7.19.3</A>).  The currently possible modes:

<P>
<div class="center"><table class="realtable">
  <thead>
    <tr>
      <th class="center">Mode</th>
      <th class="left"  >Action</th>
      </tr>
    </thead>
  <tbody>
    <tr><td class="center" valign="baseline"><code>'r|'</code></td>
        <td class="left"  >Open a <em>stream</em> of uncompressed tar blocks for reading.</td></tr>
    <tr><td class="center" valign="baseline"><code>'r|gz'</code></td>
        <td class="left"  >Open a gzip compressed <em>stream</em> for reading.</td></tr>
    <tr><td class="center" valign="baseline"><code>'r|bz2'</code></td>
        <td class="left"  >Open a bzip2 compressed <em>stream</em> for reading.</td></tr>
    <tr><td class="center" valign="baseline"><code>'w|'</code></td>
        <td class="left"  >Open an uncompressed <em>stream</em> for writing.</td></tr>
    <tr><td class="center" valign="baseline"><code>'w|gz'</code></td>
        <td class="left"  >Open an gzip compressed <em>stream</em> for writing.</td></tr>
    <tr><td class="center" valign="baseline"><code>'w|bz2'</code></td>
        <td class="left"  >Open an bzip2 compressed <em>stream</em> for writing.</td></tr></tbody>
</table></div>
</dl>

<P>
<dl><dt><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-2890' xml:id='l2h-2890' class="class">TarFile</tt></b>
<dd>
    Class for reading and writing tar archives. Do not use this
    class directly, better use <tt class="function">open()</tt> instead.
    See ``TarFile Objects'' (section&nbsp;<A href="tarfile-objects.html#tarfile-objects">7.19.1</A>).
</dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2891' xml:id='l2h-2891' class="function">is_tarfile</tt></b>(</nobr></td>
  <td><var>name</var>)</td></tr></table></dt>
<dd>
    Return <tt class="constant">True</tt> if <var>name</var> is a tar archive file, that
    the <tt class="module">tarfile</tt> module can read.
</dl>

<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-2892' xml:id='l2h-2892' class="class">TarFileCompat</tt></b>(</nobr></td>
  <td><var>filename</var><big>[</big><var>, mode</var><big>[</big><var>,
                                 compression</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
    Class for limited access to tar archives with a
    <tt class="module"><a href="module-zipfile.html">zipfile</a></tt>-like interface. Please consult the
    documentation of the <tt class="module"><a href="module-zipfile.html">zipfile</a></tt> module for more details.
    <var>compression</var> must be one of the following constants:
    <dl><dt><b><tt id='l2h-2893' xml:id='l2h-2893'>TAR_PLAIN</tt></b></dt>
<dd>
        Constant for an uncompressed tar archive.
    </dd></dl>
    <dl><dt><b><tt id='l2h-2894' xml:id='l2h-2894'>TAR_GZIPPED</tt></b></dt>
<dd>
        Constant for a <tt class="module"><a href="module-gzip.html">gzip</a></tt> compressed tar archive.
    </dd></dl>
</dl>

<P>
<dl><dt><b><span class="typelabel">exception</span>&nbsp;<tt id='l2h-2895' xml:id='l2h-2895' class="exception">TarError</tt></b></dt>
<dd>
    Base class for all <tt class="module">tarfile</tt> exceptions.
</dd></dl>

<P>
<dl><dt><b><span class="typelabel">exception</span>&nbsp;<tt id='l2h-2896' xml:id='l2h-2896' class="exception">ReadError</tt></b></dt>
<dd>
    Is raised when a tar archive is opened, that either cannot be handled by
    the <tt class="module">tarfile</tt> module or is somehow invalid.
</dd></dl>

<P>
<dl><dt><b><span class="typelabel">exception</span>&nbsp;<tt id='l2h-2897' xml:id='l2h-2897' class="exception">CompressionError</tt></b></dt>
<dd>
    Is raised when a compression method is not supported or when the data
    cannot be decoded properly.
</dd></dl>

<P>
<dl><dt><b><span class="typelabel">exception</span>&nbsp;<tt id='l2h-2898' xml:id='l2h-2898' class="exception">StreamError</tt></b></dt>
<dd>
    Is raised for the limitations that are typical for stream-like
    <tt class="class">TarFile</tt> objects.
</dd></dl>

<P>
<dl><dt><b><span class="typelabel">exception</span>&nbsp;<tt id='l2h-2899' xml:id='l2h-2899' class="exception">ExtractError</tt></b></dt>
<dd>
    Is raised for <em>non-fatal</em> errors when using <tt class="method">extract()</tt>, but
    only if <tt class="member">TarFile.errorlevel</tt><code> == 2</code>.
</dd></dl>

<P>
<div class="seealso">
  <p class="heading">See Also:</p>

    <dl compact="compact" class="seemodule">
    <dt>Module <b><tt class="module"><a href="module-zipfile.html">zipfile</a></tt>:</b>
    <dd>Documentation of the <tt class="module"><a href="module-zipfile.html">zipfile</a></tt>
    standard module.
  </dl>

<P>
<dl compact="compact" class="seetitle">
    <dt><em class="citetitle"><a href="http://www.gnu.org/software/tar/manual/html_chapter/tar_8.html#SEC134"
        >GNU tar manual, Basic Tar Format</a></em></dt>
    <dd>Documentation for tar archive files,
    including GNU tar extensions.</dd>
  </dl>
</div>

<P>

<p><br /></p><hr class='online-navigation' />
<div class='online-navigation'>
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a>

<UL CLASS="ChildLinks">
<LI><A href="tarfile-objects.html">7.19.1 TarFile Objects</a>
<LI><A href="tarinfo-objects.html">7.19.2 TarInfo Objects</a>
<LI><A href="tar-examples.html">7.19.3 Examples</a>
</ul>
<!--End of Table of Child-Links-->
</div>

<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="7.18.3 ZipInfo Objects"
  href="zipinfo-objects.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="7. Optional Operating System"
  href="someos.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="7.19.1 TarFile Objects"
  href="tarfile-objects.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="zipinfo-objects.html">7.18.3 ZipInfo Objects</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="someos.html">7. Optional Operating System</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="tarfile-objects.html">7.19.1 TarFile Objects</A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.3, documentation updated on 29 March 2006.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>