Python Glossary Русский

all Python 3tarfileexception

tarfile.HeaderError

exceptions

The tarfile exception for a member header (a 512-byte block) that could not be parsed.

Syntax

except tarfile.HeaderError:

TarInfo.frombuf() raises it directly; from the outside, opening a broken archive, you see ReadError instead. Internal subclasses narrow down the case — empty, truncated or invalid header; they are undocumented, so catch HeaderError itself rather than them. Inside the module it has five undocumented subclasses — EOFHeaderError, EmptyHeaderError, InvalidHeaderError, SubsequentHeaderError, TruncatedHeaderError; catch HeaderError.

Examples

import tarfile
for raw in (b'x' * 100, b'x' * 512):
    try:
        tarfile.TarInfo.frombuf(raw, 'utf-8', 'surrogateescape')
    except tarfile.HeaderError as e:
        print(e)
# → truncated header / invalid header

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy