Bad magic number error with ZipFile module in Python

前端 未结 2 1074
轻奢々
轻奢々 2020-12-11 16:19

I am using Python 2.7 on Windows 7 (64 bit). When I try to unzip a zip file with ZipFile module I get the following error:-

Traceback (most recent call last)         


        
2条回答
  •  轮回少年
    2020-12-11 16:40

    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')
    

提交回复
热议问题