ASPN ActiveState Programmer Network
  ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups | Web Services
SEARCH
advanced | search help

Reference
ActivePython 2.4
Python Documentation
Library Reference
7. Optional Operating System Services
7.1 signal -- Set handlers for asynchronous events
7.2 socket -- Low-level networking interface
7.3 select -- Waiting for I/O completion
7.4 thread -- Multiple threads of control
7.5 threading -- Higher-level threading interface
7.6 dummy thread -- Drop-in replacement for the thread module
7.7 dummy threading -- Drop-in replacement for the threading module
7.8 Queue -- A synchronized queue class
7.9 mmap -- Memory-mapped file support
7.10 anydbm -- Generic access to DBM-style databases
7.11 dbhash -- DBM-style interface to the BSD database library
7.12 whichdb -- Guess which DBM module created a database
7.13 bsddb -- Interface to Berkeley DB library
7.14 dumbdbm -- Portable DBM implementation
7.15 zlib -- Compression compatible with gzip
7.16 gzip -- Support for gzip files
7.17 bz2 -- Compression compatible with bzip2
7.18 zipfile -- Work with ZIP archives
7.19 tarfile -- Read and write tar archive files
7.19.1 TarFile Objects
7.19.2 TarInfo Objects
7.19.3 Examples
7.20 readline -- GNU readline interface
7.21 rlcompleter -- Completion function for GNU readline

MyASPN >> Reference >> ActivePython 2.4 >> Python Documentation >> Library Reference >> 7. Optional Operating System Services
ActivePython 2.4 documentation

7.19 tarfile -- Read and write tar archive files

New in version 2.3.

The tarfile module makes it possible to read and create tar archives. Some facts and figures:

  • reads and writes gzip and bzip2 compressed archives.
  • creates POSIX 1003.1-1990 compliant or GNU tar compatible archives.
  • reads GNU tar extensions longname, longlink and sparse.
  • stores pathnames of unlimited length using GNU tar extensions.
  • 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.
  • can handle tape devices.

open( [name[, mode [, fileobj[, bufsize]]]])
Return a TarFile object for the pathname name. For detailed information on TarFile objects, see TarFile Objects (section 7.19.1).

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

mode action
'r' Open for reading with transparent compression (recommended).
'r:' Open for reading exclusively without compression.
'r:gz' Open for reading with gzip compression.
'r:bz2' Open for reading with bzip2 compression.
'a' or 'a:' Open for appending with no compression.
'w' or 'w:' Open for uncompressed writing.
'w:gz' Open for gzip compressed writing.
'w:bz2' Open for bzip2 compressed writing.

Note that 'a:gz' or 'a:bz2' is not possible. If mode is not suitable to open a certain (compressed) file for reading, ReadError is raised. Use mode 'r' to avoid this. If a compression method is not supported, CompressionError is raised.

If fileobj is specified, it is used as an alternative to a file object opened for name.

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

Mode Action
'r|' Open a stream of uncompressed tar blocks for reading.
'r|gz' Open a gzip compressed stream for reading.
'r|bz2' Open a bzip2 compressed stream for reading.
'w|' Open an uncompressed stream for writing.
'w|gz' Open an gzip compressed stream for writing.
'w|bz2' Open an bzip2 compressed stream for writing.

class TarFile
Class for reading and writing tar archives. Do not use this class directly, better use open() instead. See ``TarFile Objects'' (section 7.19.1).

is_tarfile( name)
Return True if name is a tar archive file, that the tarfile module can read.

class TarFileCompat( filename[, mode[, compression]])
Class for limited access to tar archives with a zipfile-like interface. Please consult the documentation of the zipfile module for more details. compression must be one of the following constants:
TAR_PLAIN
Constant for an uncompressed tar archive.
TAR_GZIPPED
Constant for a gzip compressed tar archive.

exception TarError
Base class for all tarfile exceptions.

exception ReadError
Is raised when a tar archive is opened, that either cannot be handled by the tarfile module or is somehow invalid.

exception CompressionError
Is raised when a compression method is not supported or when the data cannot be decoded properly.

exception StreamError
Is raised for the limitations that are typical for stream-like TarFile objects.

exception ExtractError
Is raised for non-fatal errors when using extract(), but only if TarFile.errorlevel == 2.

See Also:

Module zipfile:
Documentation of the zipfile standard module.

GNU tar manual, Basic Tar Format
Documentation for tar archive files, including GNU tar extensions.



See About this document... for information on suggesting changes.

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState 2004 All rights reserved