TarFile Objects


TarFile methods that are compatible to zipfile:

class TarFile(file[, mode[, compression]])
Open a TAR file. file can either be a filename or a file object. mode should be 'r' for reading an existing file (default), 'a' for appending/creating a file or 'w' to create a new file replacing an existing one. Appending to a gzipped TAR file is not allowed, and raises an exception. compression must be one of the constants TAR_PLAIN for creating an uncompressed or TAR_GZIPPED for a gzipped TAR file. It defaults to TAR_PLAIN.

close()
Close the TarFile. It is recommended that you call close() before exiting your program.

getinfo(name)
Return a TarInfo object with information about archive entry name. See TarInfo Objects.

infolist()
Return a list of TarInfo Objects one for each entry in the TarFile. The list has the same order as the entries in the TarFile.

namelist()
Return a list of entries names.

printdir()
Print a table of contents to sys.stdout.

read(name)
Return the file data for the entry defined by name.

testtar() or testzip()
Test all entries in the TarFile. Return the name of the first bad file, else None.

write(filename[, arcname[, compress_type[, recursive]]])
Write file filename to the TarFile giving it the name arcname in the TarFile (optional). If filename is the name of a directory, all files in this directory and its subdirectories are written to the TarFile. If recursive is false only the directory is written to the TarFile, without adding any underlying file. recursive is true by default. compress_type exists for compatibility to zipfile but has no effect. The TarFile must be opened with mode 'a' or 'w'.
Note: File permissions, mtime, userid, groupid etc. are added implicitly.

writestr(tarinfo, bytes)
Write bytes to the TarFile. tarinfo is a TarInfo instance that provides additional information about bytes. At least filename and date_time must be given. The TarFile must be opened with mode 'a' or 'w'.

TarFile provides some additional methods, which can not be found in zipfile:

extract(name / tarinfo[, path])
Extract a TarFile entry determined either by its name or its TarInfo Object to current working directory or to path (if given). This method is recommended over read(), because it additionally extracts detailed file information like file type, file permissions, userid etc. See TarInfo for more details.

next()
Return the next entry in the TarFile as a TarInfo instance. Return None if there is no more entry available. This method is intended to be used in a while statement.
In contrast to ZIP and other archive formats, TAR files do not contain a central directory structure. Therefore, the TarFile Class has to go through the entire archive at startup to gain information about the entries. This can take some time when the archive contains a large number of files. If you use the next() method, you can go through the archive entry by entry, and reading in the contents at startup is avoided.

__iter__()
TarFile furthermore makes it possible to go through its entries by Iteration. For example, you can write some code like this:
for tarinfo in TarFile(...):
	[...]
	
This feature is first introduced in Python version 2.2. Users of Python less than 2.2 can use the next() method in combination with while described above.

TarFile provides the following class attributes:

debug
Set the debuglevel to a value between 0 (no messages, default) and 3 (all messages). The debugging output is written to sys.stdout.

followsymlinks
If set to true (default), symbolic links are resolved when archived. That means that the file to which the symlink is pointing is archived, not the symlink itself.



Copyright © 2002 Lars Gustäbel lars@gustaebel.de