Python Glossary Русский

Python 3.2+zipfileexception

zipfile.BadZipFile

exceptions

The zipfile exception for a file that is not a ZIP archive or is corrupt (for example, a CRC mismatch on read).

Syntax

except zipfile.BadZipFile:

It derives from Exception, not OSError: except OSError will not catch it even though the error is about a file. It is raised both on opening (no end-of-archive record) and later, by read() of a member with corrupt data, so one try around ZipFile(...) is not enough. The old name BadZipfile (lowercase f) is the same class kept for compatibility. A missing name inside the archive is a KeyError, not BadZipFile.

Examples

import zipfile
with open('fake.zip', 'w', encoding='utf-8') as f:
    f.write('это не архив')
try:
    zipfile.ZipFile('fake.zip')
except zipfile.BadZipFile as e:
    print(e)  # → File is not a zip file
print(issubclass(zipfile.BadZipFile, OSError))  # → False

See also

Open in the full glossarydocs.python.orgReport an inaccuracy