tarfile — Read and write tar archive files¶
Source code: Lib/tarfile.py
The tarfile module makes it possible to read and write tar
archives, including those using gzip, bz2 and lzma compression.
Use the zipfile module to read or write .zip files, or the
higher-level functions in shutil.
Some facts and figures:
reads and writes
gzip,bz2,compression.zstd, andlzmacompressed archives if the respective modules are available.If any of these optional modules are missing from your copy of CPython, look for documentation from your distributor (that is, whoever provided Python to you). If you are the distributor, see Requirements for optional modules.
read/write support for the POSIX.1-1988 (ustar) format.
read/write support for the GNU tar format including longname and longlink extensions, read-only support for all variants of the sparse extension including restoration of sparse files.
read/write support for the POSIX.1-2001 (pax) format.
handles directories, regular files, hardlinks, symbolic links, fifos, character devices and block devices and is able to acquire and restore file information like timestamp, access permissions and owner.
Changed in version 3.3: Added support for lzma compression.
Changed in version 3.12: Archives are extracted using a filter, which makes it possible to either limit surprising/dangerous features, or to acknowledge that they are expected and the archive is fully trusted.
Changed in version 3.14: Set the default extraction filter to data,
which disallows some dangerous features such as links to absolute paths
or paths outside of the destination. Previously, the filter strategy
was equivalent to fully_trusted.
Changed in version 3.14: Added support for Zstandard compression using compression.zstd.
- tarfile.open(name=None, mode='r', fileobj=None, bufsize=10240, **kwargs)¶
Return a
TarFileobject for the pathname name. For detailed information onTarFileobjects and the keyword arguments that are allowed, see TarFile Objects.mode has to be a string of the form
'filemode[:compression]', it defaults to'r'. Here is a full list of mode combinations:mode
action
'r'or'r:*'Open for reading with transparent compression (recommended).
'r:'Open for reading exclusively without compression.
'r:gz'Open for reading with gzip compression.
'r:bz2'Open for reading with bzip2 compression.
'r:xz'Open for reading with lzma compression.
'r:zst'Open for reading with Zstandard compression.
'x'or'x:'Create a tarfile exclusively without compression. Raise a
FileExistsErrorexception if it already exists.'x:gz'Create a tarfile with gzip compression. Raise a
FileExistsErrorexception if it already exists.'x:bz2'Create a tarfile with bzip2 compression. Raise a
FileExistsErrorexception if it already exists.'x:xz'Create a tarfile with lzma compression. Raise a
FileExistsErrorexception if it already exists.'x:zst'Create a tarfile with Zstandard compression. Raise a
FileExistsErrorexception if it already exists.'a'or'a:'Open for appending with no compression. The file is created if it does not exist.
'w'or'w:'Open for uncompressed writing.
'w:gz'Open for gzip compressed writing.
'w:bz2'Open for bzip2 compressed writing.
'w:xz'Open for lzma compressed writing.
'w:zst'Open for Zstandard compressed writing.
Note that
'a:gz','a:bz2'or'a:xz'is not possible. If mode is not suitable to open a certain (compressed) file for reading,ReadErroris raised. Use mode'r'to avoid this. If a compression method is not supported,CompressionErroris raised.Note
Compressed archives opened with modes like
'r:gz','r:bz2','r:xz', or'r:zst'support random access, but seeking backwards in the underlying compressed stream requires re-decompressing from the beginning. Extracting members in a different order than they appear in the archive can therefore be significantly slower — proportional to the total compressed data read rather than just the target member’s size. For best performance, extract members in archive order or useTarFile.extractall().If fileobj is specified, it is used as an alternative to a file object opened in binary mode for name. It is supposed to be at position 0.
For modes
'w:gz','x:gz','w|gz','w:bz2','x:bz2','w|bz2',tarfile.open()accepts the keyword argument compresslevel (default6) to specify the compression level of the file.For modes
'w:xz','x:xz'and'w|xz',tarfile.open()accepts the keyword argument preset to specify the compression level of the file.For modes
'w:zst','x:zst'and'w|zst',tarfile.open()accepts the keyword argument level to specify the compression level of the file. The keyword argument options may also be passed, providing advanced Zstandard compression parameters described byCompressionParameter. The keyword argument zstd_dict can be passed to provide aZstdDict, a Zstandard dictionary used to improve compression of smaller amounts of data.For modes
'w:gz'and'w|gz',tarfile.open()accepts the keyword argument mtime to create a gzip archive header with that mtime. By default, the mtime is set to the time of creation of the archive. Use mtime0to generate a compressed stream that does not depend on creation time, for reproducible output.For special purposes, there is a second format for mode:
'filemode|[compression]'.tarfile.open()will return aTarFileobject that processes its data as a stream of blocks. No random seeking will be done on the file. If given, fileobj may be any object that has aread()orwrite()method (depending on the mode) that works with bytes. bufsize specifies the blocksize and defaults to20 * 512bytes. Use this variant in combination with e.g.sys.stdin.buffer, a socket file object or a tape device. However, such aTarFileobject is limited in that it does not allow random access, see Examples. The currently possible modes:Mode
Action
'r|*'Open a stream of tar blocks for reading with transparent compression.
'r|'Open a stream of uncompressed tar blocks for reading.
'r|gz'Open a gzip compressed stream for reading.
'r|bz2'Open a bzip2 compressed stream for reading.
'r|xz'Open an lzma compressed stream for reading.
'r|zst'Open a Zstandard compressed stream for reading.
'w|'Open an uncompressed stream for writing.
'w|gz'Open a gzip compressed stream for writing.
'w|bz2'Open a bzip2 compressed stream for writing.
'w|xz'Open an lzma compressed stream for writing.
'w|zst'Open a Zstandard compressed stream for writing.
Changed in version 3.5: The
'x'(exclusive creation) mode was added.Changed in version 3.6: The name parameter accepts a path-like object.
Changed in version 3.12: The compresslevel keyword argument also works for streams.
Changed in version 3.14: The preset keyword argument also works for streams.
Changed in version 3.15: The default compression level was reduced to 6 (down from 9). It is the default level used by most compression tools and a better tradeoff between speed and performance.
- class tarfile.TarFile
Class for reading and writing tar archives. Do not use this class directly: use
tarfile.open()instead. See TarFile Objects.
- tarfile.is_tarfile(name)¶
Return
Trueif name is a tar archive file, that thetarfilemodule can read. name may be astr, file, or file-like object.Changed in version 3.9: Support for file and file-like objects.
The tarfile module defines the following exceptions:
- exception tarfile.TarError¶
Base class for all
tarfileexceptions.
- exception tarfile.ReadError¶
Is raised when a tar archive is opened, that either cannot be handled by the
tarfilemodule or is somehow invalid.
- exception tarfile.CompressionError¶
Is raised when a compression method is not supported or when the data cannot be decoded properly.
- exception tarfile.StreamError¶
Is raised for the limitations that are typical for stream-like
TarFileobjects.
- exception tarfile.ExtractError¶
Is raised for non-fatal errors when using
TarFile.extract(), but only ifTarFile.errorlevel== 2.
- exception tarfile.HeaderError¶
Is raised by
TarInfo.frombuf()if the buffer it gets is invalid.
- exception tarfile.AbsolutePathError¶
Raised to refuse extracting a member with an absolute path.
- exception tarfile.OutsideDestinationError¶
Raised to refuse extracting a member outside the destination directory.
- exception tarfile.SpecialFileError¶
Raised to refuse extracting a special file (e.g. a device or pipe).
- exception tarfile.AbsoluteLinkError¶
Raised to refuse extracting a symbolic link with an absolute path.
- exception tarfile.LinkOutsideDestinationError¶
Raised to refuse extracting a symbolic link pointing outside the destination directory.
- exception tarfile.LinkFallbackError¶
Raised to refuse emulating a link (hard or symbolic) by extracting another archive member, when that member would be rejected by the filter location. The exception that was raised to reject the replacement member is available as
BaseException.__context__.Added in version 3.15.
The following constants are available at the module level:
- tarfile.ENCODING¶
The default character encoding:
'utf-8'on Windows, the value returned bysys.getfilesystemencoding()otherwise.
Each of the following constants defines a tar archive format that the
tarfile module is able to create. See section Supported tar formats for
details.
- tarfile.USTAR_FORMAT¶
POSIX.1-1988 (ustar) format.
- tarfile.GNU_FORMAT¶
GNU tar format.
- tarfile.PAX_FORMAT¶
POSIX.1-2001 (pax) format.
- tarfile.DEFAULT_FORMAT¶
The default format for creating archives. This is currently
PAX_FORMAT.Changed in version 3.8: The default format for new archives was changed to
PAX_FORMATfromGNU_FORMAT.
See also
- Module
zipfile Documentation of the
zipfilestandard module.- Archiving operations
Documentation of the higher-level archiving facilities provided by the standard
shutilmodule.- GNU tar manual, Basic Tar Format
Documentation for tar archive files, including GNU tar extensions.
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 for details.
A TarFile object can be used as a context manager in a with
statement. It will automatically be closed when the block is completed. Please
note that in the event of an exception an archive opened for writing will not
be finalized; only the internally used file object will be closed. See the
Examples section for a use case.
Added in version 3.2: Added support for the context management protocol.
- class tarfile.TarFile(name=None, mode='r', fileobj=None, format=DEFAULT_FORMAT, tarinfo=TarInfo, dereference=False, ignore_zeros=False, encoding=ENCODING, errors='surrogateescape', pax_headers=None, debug=0, errorlevel=1, stream=False)¶
All following arguments are optional and can be accessed as instance attributes as well.
name is the pathname of the archive. name may be a path-like object. It can be omitted if fileobj is given. In this case, the file object’s
nameattribute is used if it exists.mode is either
'r'to read from an existing archive,'a'to append data to an existing file,'w'to create a new file overwriting an existing one, or'x'to create a new file only if it does not already exist.If fileobj is given, it is used for reading or writing data. If it can be determined, mode is overridden by fileobj’s mode. fileobj will be used from position 0.
Note
fileobj is not closed, when
TarFileis closed.format controls the archive format for writing. It must be one of the constants
USTAR_FORMAT,GNU_FORMATorPAX_FORMATthat are defined at module level. When reading, format will be automatically detected, even if different formats are present in a single archive.The tarinfo argument can be used to replace the default
TarInfoclass with a different one.If dereference is
False, add symbolic and hard links to the archive. If it isTrue, add the content of the target files to the archive. This has no effect on systems that do not support symbolic links.If ignore_zeros is
False, treat an empty block as the end of the archive. If it isTrue, skip empty (and invalid) blocks and try to get as many members as possible. This is only useful for reading concatenated or damaged archives.debug can be set from
0(no debug messages) up to3(all debug messages). The messages are written tosys.stderr.errorlevel controls how extraction errors are handled, see
the corresponding attribute.The encoding and errors arguments define the character encoding to be used for reading or writing the archive and how conversion errors are going to be handled. The default settings will work for most users. See section Unicode issues for in-depth information.
The pax_headers argument is an optional dictionary of strings which will be added as a pax global header if format is
PAX_FORMAT.If stream is set to
Truethen while reading the archive info about files in the archive are not cached, saving memory.Changed in version 3.2: Use
'surrogateescape'as the default for the errors argument.Changed in version 3.5: The
'x'(exclusive creation) mode was added.Changed in version 3.6: The name parameter accepts a path-like object.
Changed in version 3.13: Add the stream parameter.
- classmethod TarFile.open(...)¶
Alternative constructor. The
tarfile.open()function is actually a shortcut to this classmethod.
- TarFile.getmember(name)¶
Return a
TarInfoobject for member name. If name can not be found in the archive,KeyErroris 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.
- TarFile.getmembers()¶
Return the members of the archive as a list of
TarInfoobjects. The list has the same order as the members in the archive.
- TarFile.getnames()¶
Return the members as a list of their names. It has the same order as the list returned by
getmembers().