7.19.1 TarFile Objects
The TarFile object provides an interface to a tar archive. A tar
archive is a sequence of blocks. An archive member (a stored file) is made up
of a header block followed by data blocks. It is possible, to store a file in a
tar archive several times. Each archive member is represented by a
TarInfo object, see TarInfo Objects (section
7.19.2) for details.
class TarFile( |
[name
[, mode[, fileobj]]]) |
-
Open an (uncompressed) tar archive name.
mode is either
'r'
to read from an existing archive,
'a'
to append data to an existing file or 'w'
to create a new
file overwriting an existing one. mode defaults to 'r'
.
If fileobj is given, it is used for reading or writing data.
If it can be determined, mode is overridden by fileobj's mode.
Note:
fileobj is not closed, when TarFile is closed.
-
Alternative constructor. The open() function on module level is
actually a shortcut to this classmethod. See section 7.19
for details.
-
Return a TarInfo object for member name. If name can
not be found in the archive, KeyError is raised.
Note:
If a member occurs more than once in the archive, its last
occurrence is assumed to be the most up-to-date version.
-
Return the members of the archive as a list of TarInfo objects.
The list has the same order as the members in the archive.
-
Return the members as a list of their names. It has the same order as
the list returned by getmembers().
-
Print a table of contents to
sys.stdout
. If verbose is
False, only the names of the members are printed. If it is
True, output similar to that of ls -l is produced.
-
Return the next member of the archive as a TarInfo object, when
TarFile is opened for reading. Return
None
if there is no
more available.
-
Extract a member from the archive to the current working directory,
using its full name. Its file information is extracted as accurately as
possible.
member may be a filename or a TarInfo object.
You can specify a different directory using path.
-
Extract a member from the archive as a file object.
member may be a filename or a TarInfo object.
If member is a regular file, a file-like object is returned.
If member is a link, a file-like object is constructed from the
link's target.
If member is none of the above,
None
is returned.
Note:
The file-like object is read-only and provides the following methods:
read(), readline(), readlines(),
seek(), tell().
add( |
name[, arcname[, recursive]]) |
-
Add the file name to the archive. name may be any type
of file (directory, fifo, symbolic link, etc.).
If given, arcname specifies an alternative name for the file in the
archive. Directories are added recursively by default.
This can be avoided by setting recursive to False;
the default is True.
addfile( |
tarinfo[, fileobj]) |
-
Add the TarInfo object tarinfo to the archive.
If fileobj is given,
tarinfo.size
bytes are read
from it and added to the archive. You can create TarInfo objects
using gettarinfo().
Note:
On Windows platforms, fileobj should always be opened with mode
'rb'
to avoid irritation about the file size.
gettarinfo( |
[name[,
arcname[, fileobj]]]) |
-
Create a TarInfo object for either the file name or
the file object fileobj (using os.fstat() on its
file descriptor). You can modify some of the TarInfo's
attributes before you add it using addfile(). If given,
arcname specifies an alternative name for the file in the
archive.
-
Close the TarFile. In write mode, two finishing zero
blocks are appended to the archive.
- posix
-
If true, create a POSIX 1003.1-1990 compliant archive. GNU
extensions are not used, because they are not part of the POSIX
standard. This limits the length of filenames to at most 256,
link names to 100 characters and the maximum file size to 8
gigabytes. A ValueError is raised if a file exceeds
this limit. If false, create a GNU tar compatible archive. It
will not be POSIX compliant, but can store files without any
of the above restrictions.
Changed in version 2.4:
posix defaults to False.
- dereference
-
If false, add symbolic and hard links to archive. If true, add the
content of the target files to the archive. This has no effect on
systems that do not support symbolic links.
- ignore_zeros
-
If false, treat an empty block as the end of the archive. If true,
skip empty (and invalid) blocks and try to get as many members as
possible. This is only useful for concatenated or damaged
archives.
- debug=0
-
To be set from
0
(no debug messages; the default) up to
3
(all debug messages). The messages are written to
sys.stdout
.
- errorlevel
-
If
0
(the default), all errors are ignored when using
extract(). Nevertheless, they appear as error messages
in the debug output, when debugging is enabled. If 1
, all
fatal errors are raised as OSError or
IOError exceptions. If 2
, all non-fatal
errors are raised as TarError exceptions as well.
Release 2.4, documentation updated on 30 November 2004.
See About this document... for information on suggesting changes.