Bad magic number error with ZipFile module in Python

旧街凉风 提交于 2019-12-01 17:07:06

Correct ZIP files always have "\x50\x4B\x03\x04" in the beginning. You can test whether file is really ZIP file with this code:

with open('/path/to/file', 'rb') as MyZip:
  print(MyZip.read(4))

It will print header of file so you can check.

UPDATE Strange, testzip() and all other functions work good. Had you tried such code?

with zipfile.GzipFile('/path/to/file') as Zip:
  for ZipMember in Zip.infolist():
    Zip.extract(ZipMember, path='/dir/where/to/extract', pwd='your-password')

Make sure you are really opening a ZIP file, not for example a RAR file named with a .zip extension. Proper zip files have a header, which was not found in this case.

The zipfile module can only open zip files. WinRAR can also open other formats, and it likely ignores the filename and only looks at the file itself.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!