all Python 3Exceptionsexception
struct.error
module exceptions
A struct packing or unpacking error (the format does not match the data).
Syntax
raise struct.error
Two causes dominate: unpack got a buffer whose length differs from struct.calcsize(fmt) — use unpack_from to read just a prefix — or a value that does not fit its format code, like 300 into 'B'. Note also that the default layout is native with padding; the '<', '>' and '=' prefixes give you fixed, predictable sizes. It inherits straight from Exception, not from ValueError, so a habitual except ValueError misses it.
Examples
import struct
try:
raise struct.error
except struct.error as e:
print(type(e).__name__) # → error
See also
Open in the full glossarydocs.python.orgReport an inaccuracy