<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<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>
<div id='top-navigation-panel' xml:id='top-navigation-panel'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<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>
<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>
<!--End of Navigation Panel-->
<H1><A NAME=
"SECTION0091900000000000000000">
7.19 <tt class=
"module">tarfile
</tt> -- Read and write tar archive files
</A>
<A NAME=
"module-tarfile"></A>
<span class=
"versionnote">New in version
2.3.
</span>
The
<tt class=
"module">tarfile
</tt> module makes it possible to read and create tar archives.
<LI>reads and writes
<tt class=
"module">gzip
</tt> and
<tt class=
"module">bzip2
</tt> compressed archives.
<LI>creates POSIX
1003.1-
1990 compliant or GNU tar compatible archives.
<LI>reads GNU tar extensions
<em>longname
</em>,
<em>longlink
</em> and
<LI>stores pathnames of unlimited length using GNU tar extensions.
<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>can handle tape devices.
<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>
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>).
<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:
<div class=
"center"><table class=
"realtable">
<th class=
"center">mode
</th>
<th class=
"left" >action
</th>
<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>
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.
If
<var>fileobj
</var> is specified, it is used as an alternative to
a file object opened for
<var>name
</var>.
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
<A href=
"tar-examples.html#tar-examples">7.19.3</A>). The currently possible modes:
<div class=
"center"><table class=
"realtable">
<th class=
"center">Mode
</th>
<th class=
"left" >Action
</th>
<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>
<dl><dt><b><span class=
"typelabel">class
</span> <tt id='l2h-
2890' xml:id='l2h-
2890'
class=
"class">TarFile
</tt></b>
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
<A href=
"tarfile-objects.html#tarfile-objects">7.19.1</A>).
<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>
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><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><span class=
"typelabel">class
</span> <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>
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>
Constant for an uncompressed tar archive.
<dl><dt><b><tt id='l2h-
2894' xml:id='l2h-
2894'
>TAR_GZIPPED
</tt></b></dt>
Constant for a
<tt class=
"module"><a href=
"module-gzip.html">gzip
</a></tt> compressed tar archive.
<dl><dt><b><span class=
"typelabel">exception
</span> <tt id='l2h-
2895' xml:id='l2h-
2895'
class=
"exception">TarError
</tt></b></dt>
Base class for all
<tt class=
"module">tarfile
</tt> exceptions.
<dl><dt><b><span class=
"typelabel">exception
</span> <tt id='l2h-
2896' xml:id='l2h-
2896'
class=
"exception">ReadError
</tt></b></dt>
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.
<dl><dt><b><span class=
"typelabel">exception
</span> <tt id='l2h-
2897' xml:id='l2h-
2897'
class=
"exception">CompressionError
</tt></b></dt>
Is raised when a compression method is not supported or when the data
cannot be decoded properly.
<dl><dt><b><span class=
"typelabel">exception
</span> <tt id='l2h-
2898' xml:id='l2h-
2898'
class=
"exception">StreamError
</tt></b></dt>
Is raised for the limitations that are typical for stream-like
<tt class=
"class">TarFile
</tt> objects.
<dl><dt><b><span class=
"typelabel">exception
</span> <tt id='l2h-
2899' xml:id='l2h-
2899'
class=
"exception">ExtractError
</tt></b></dt>
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>.
<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>
<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>
<p><br /></p><hr class='online-navigation'
/>
<div class='online-navigation'
>
<!--Table of Child-Links-->
<A NAME=
"CHILD_LINKS"><STRONG>Subsections
</STRONG></a>
<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>
<!--End of Table of Child-Links-->
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<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>
<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>
<span class=
"release-info">Release
2.4.2, documentation updated on
28 September
2005.
</span>
<!--End of Navigation Panel-->
See
<i><a href=
"about.html">About this document...
</a></i> for information on suggesting changes.