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)
||[name [, mode[, fileobj]]])
- Open an (uncompressed) tar archive name. mode is either
to read from an existing archive,
'a' to append data to an existing file or
to create a new file overwriting an existing one. mode defaults to
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
- 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
occurence 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
- Print a table of contents to
sys.stdout. If verbose is
only the names of the members are printed. If it is
-l"-like output 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
- 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
- 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,
Note: The file-like object is read-only and provides the following
methods: read(), readline(), readlines(), seek(), tell().
||name[, arcname[, recursive=True]])
- 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
- Add the TarInfo object tarinfo to the archive. If fileobj
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.
||[name[, arcname [,
- 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.
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 and linknames to 100 characters. A ValueError
is raised, if a pathname exceeds this limit. If
False, create a GNU tar
compatible archive. It will not be POSIX compliant, but can store pathnames of unlimited
False, add symbolic and hard links to archive. If
the content of the target files to the archive. This has no effect on systems that do not
False, treat an empty block as the end of the archive. If
skip empty (and invalid) blocks and try to get as many members as possible. This is only
useful for concatenated or damaged archives.
- To be set from
0(no debug messages) up to
messages). The messages are written to
0, all errors are ignored when using extract().
Nevertheless, they appear as error messages in the debug output, when debugging is
1, all fatal errors are raised as OSError
or IOError exceptions. If
2, all non-fatal
errors are raised as TarError exceptions as well.