7.19.1 TarFile Objects
Description: TarFile Objects
Keywords: lib

##
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.

`open`( | ...)

  Alternative constructor. The `open()` function on module level is actually a shortcut to this classmethod. See section 7.19 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 occurence 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`, an `"ls -l"`-like output 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.

`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.

`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=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 `False`.

`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=True`
  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 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 length.

`dereference=False`
  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 links.

`ignore_zeros=False`
  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) up to `3`(all debug messages). The messages are written to `sys.stdout`.

`errorlevel=0`
  If `0`, 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.