hal*_*tTm 8 python unzip zipfile python-2.7
我在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
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
这段代码适用于我使用WinRAR创建的许多其他zip文件 myzip.zip
我尝试评论以下几行Python27\Lib\zipfile.py: -
if fheader[0:4] != stringFileHeader:
raise BadZipfile, "Bad magic number for file header"
Run Code Online (Sandbox Code Playgroud)
但这并没有真正帮助.运行我的代码实际上,我在我的shell上得到一些转储.
gho*_*nsd 10
正确的ZIP文件在开头总是有"\ x50\x4B\x03\x04".您可以使用以下代码测试文件是否真的是ZIP文件:
with open('/path/to/file', 'rb') as MyZip:
print(MyZip.read(4))
Run Code Online (Sandbox Code Playgroud)
它将打印文件头,以便您检查.
UPDATE Strange,testzip()和所有其他函数都运行良好.你试过这样的代码吗?
with zipfile.GzipFile('/path/to/file') as Zip:
for ZipMember in Zip.infolist():
Zip.extract(ZipMember, path='/dir/where/to/extract', pwd='your-password')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16138 次 |
| 最近记录: |