all Python 3tarfileexception
tarfile.CompressionError
exceptions
The tarfile exception for an unknown or unavailable compression method (zlib, bz2 or lzma not built in).
Syntax
except tarfile.CompressionError:
It shows up on a typo in the mode ('w:gzip' instead of 'w:gz') and on Python builds without the bz2 or lzma module — not rare in slimmed-down Docker images. Corrupt compressed data, on the other hand, usually gives ReadError. The ':zst' mode only works on versions with compression.zstd (3.14+); older ones raise CompressionError.
Examples
import tarfile
try:
tarfile.open('out.tar', 'w:gzip')
except tarfile.CompressionError as e:
print(e) # → unknown compression type 'gzip'
See also
Open in the full glossarydocs.python.orgReport an inaccuracy