Python Glossary Русский

all Python 3tarfileexception

tarfile.ReadError

exceptions

The tarfile exception for an unreadable archive — the file is not tar, is corrupt, or uses another compression than the mode says.

Syntax

except tarfile.ReadError:

The most common tarfile error. Mode 'r:gz' on an uncompressed archive gives ReadError 'not a gzip file', not CompressionError. With mode 'r' (auto-detect) the message lists every method that was tried — handy to show in a log. An empty file is a ReadError too.

Examples

import tarfile
with open('notes.txt', 'w', encoding='utf-8') as f:
    f.write('hello')
with tarfile.open('plain.tar', 'w') as tar:
    tar.add('notes.txt')
try:
    tarfile.open('plain.tar', 'r:gz')
except tarfile.ReadError as e:
    print(e)  # → not a gzip file

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy