Python 中 ZipFile 模块出现错误的幻数错误
我在 Windows 7(64 位)上使用 Python 2.7。 当我尝试使用 ZipFile 模块解压缩 zip 文件时,出现以下错误:-
Traceback (most recent call last):
File "unzip.py", line 8, in <module>
z.extract(name)
File "C:\Python27\lib\zipfile.py", line 950, in extract
return self._extract_member(member, path, pwd)
File "C:\Python27\lib\zipfile.py", line 993, in _extract_member
source = self.open(member, pwd=pwd)
File "C:\Python27\lib\zipfile.py", line 897, in open
raise BadZipfile, "Bad magic number for file header"
zipfile.BadZipfile: Bad magic number for file header
WinRAR 可以正常解压我试图解压的文件。 这是我用来从 myzip.zip
中提取文件的代码。
from zipfile import ZipFile
z = ZipFile('myzip.zip') //myzip.zip contains just one file, a password protected pdf
for name in z.namelist():
z.extract(name)
此代码对于我使用 WinRAR 创建的许多其他 zip 文件运行良好,但 myzip.zip
我尝试注释以下内容Python27\Lib\zipfile.py 中的行:-
if fheader[0:4] != stringFileHeader:
raise BadZipfile, "Bad magic number for file header"
但这并没有真正帮助。运行我的代码时,我的 shell 上会得到一些转储。
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):
File "unzip.py", line 8, in <module>
z.extract(name)
File "C:\Python27\lib\zipfile.py", line 950, in extract
return self._extract_member(member, path, pwd)
File "C:\Python27\lib\zipfile.py", line 993, in _extract_member
source = self.open(member, pwd=pwd)
File "C:\Python27\lib\zipfile.py", line 897, in open
raise BadZipfile, "Bad magic number for file header"
zipfile.BadZipfile: Bad magic number for file header
WinRAR could extract the file I am trying to extract just fine.
Here is the code I used to extract files from myzip.zip
from zipfile import ZipFile
z = ZipFile('myzip.zip') //myzip.zip contains just one file, a password protected pdf
for name in z.namelist():
z.extract(name)
This code is working fine for many other zip files I created using WinRAR but myzip.zip
I tried commenting the following lines in Python27\Lib\zipfile.py
:-
if fheader[0:4] != stringFileHeader:
raise BadZipfile, "Bad magic number for file header"
But this didn't really help. Running my code with this in effect, I get some dump on my shell.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正确的 ZIP 文件开头始终为“\x50\x4B\x03\x04”。您可以使用以下代码测试文件是否真的是 ZIP 文件:
它将打印文件头以便您检查。
更新
奇怪的是,testzip() 和所有其他函数都运行良好。你尝试过这样的代码吗?
Correct ZIP files always have "\x50\x4B\x03\x04" in the beginning. You can test whether file is really ZIP file with this code:
It will print header of file so you can check.
UPDATE
Strange, testzip() and all other functions work good. Had you tried such code?
确保您确实打开的是 ZIP 文件,而不是扩展名为 .zip 的 RAR 文件。正确的 zip 文件有一个标头,但在本例中未找到该标头。
zipfile
模块只能打开 zip 文件。 WinRAR 还可以打开其他格式,它可能会忽略文件名而只查看文件本身。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.