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