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
12.5.2) for details.
classTarFile(
[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.
open(
...)
Alternative constructor. The open() function on module level is
actually a shortcut to this classmethod. See section 12.5
for details.
getmember(
name)
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.
getmembers(
)
Return the members of the archive as a list of TarInfo objects.
The list has the same order as the members in the archive.
getnames(
)
Return the members as a list of their names. It has the same order as
the list returned by getmembers().
list(
verbose=True)
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.
next(
)
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.
extractall(
[path[, members]])
Extract all members from the archive to the current working directory
or directory path. If optional members is given, it must be
a subset of the list returned by getmembers().
Directory informations like owner, modification time and permissions are
set after all members have been extracted. This is done to work around two
problems: A directory's modification time is reset each time a file is
created in it. And, if a directory's permissions do not allow writing,
extracting files to it will fail.
New in version 2.5.
extract(
member[, path])
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.
Note:
Because the extract() method allows random access to a tar
archive there are some issues you must take care of yourself. See the
description for extractall() above.
extractfile(
member)
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(
)
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.stderr.
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.