Python Glossary Русский

all Python 3zipfilefunction

zipfile.is_zipfile()

ZIP archives

Checks whether a file (a path or a file object) looks like a ZIP archive, by the record at its end.

Syntax

zipfile.is_zipfile(filename)

It only looks at the directory record at the end of the file, not at the extension or the beginning: it returns True for .docx, .xlsx, .jar and .whl (they are ZIPs inside) and for a file with junk in front of the archive — that is how self-extracting archives work. Content integrity is not checked; corrupt data only shows up on read. A non-existent path gives False rather than an exception.

Examples

import io, zipfile
buf = io.BytesIO()
with zipfile.ZipFile(buf, 'w') as z:
    z.writestr('a.txt', '1')
with open('odd.bin', 'wb') as f:
    f.write(b'JUNK' * 10 + buf.getvalue())
print(zipfile.is_zipfile('odd.bin'))  # → True — мусор в начале не мешает
print(zipfile.is_zipfile('missing.zip'))  # → False

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy