无法解压缩 zip 文件
Can't unzip zip file
我正在尝试使用 Python 的 zipfile
模块在我的脚本中解压缩一个 zip 文件。问题是当我尝试解压缩这个文件时,它会引发 Bad magic number for file header error
:
这是error
:
..
zip_ref.extractall(destination_to_unzip_file)
File "C:\Python27\lib\zipfile.py", line 1040, in extractall
self.extract(zipinfo, path, pwd)
File "C:\Python27\lib\zipfile.py", line 1028, in extract
return self._extract_member(member, path, pwd)
File "C:\Python27\lib\zipfile.py", line 1082, in _extract_member
with self.open(member, pwd=pwd) as source, \
File "C:\Python27\lib\zipfile.py", line 971, in open
raise BadZipfile("Bad magic number for file header")
zipfile.BadZipfile: Bad magic number for file header
我要解压的文件是这样下载的:
_url = """http://edane.drsr.sk/report/ds_dphs_csv.zip"""
def download_platici_dph(self):
if os.path.isfile(_destination_for_downloads+'platici_dph.zip'):
os.remove(_destination_for_downloads+'platici_dph.zip')
with open(_destination_for_downloads+'platici_dph.zip','w') as f:
response = requests.get(_url,stream=True)
if not response.ok:
print 'Something went wrong'
return False
else:
for block in response.iter_content(1024):
f.write(block)
有人知道问题出在哪里吗?
我尝试在不使用您的 download-code 的情况下下载您的存档,然后使用以下命令解压缩:
import zipfile
with zipfile.ZipFile("ds_dphs_csv.zip") as a:
a.extractall()
它运行良好。 zipfile.BadZipfile 当 header 中出现问题时会引发异常,因此我认为您的文件在下载后已损坏。一定是你的下载方式有问题
您可以在 post 中找到有关异常的更多详细信息:Python - Extracting files from a large (6GB+) zip file
Quoth the documentation for open()
:“打开二进制文件时,您应该将 'b'
附加到模式值以在二进制模式下打开文件”。 =15=]
使用 b
为二进制文件打开输出文件:
with open(_destination_for_downloads+'platici_dph.zip','wb') as f:
我正在尝试使用 Python 的 zipfile
模块在我的脚本中解压缩一个 zip 文件。问题是当我尝试解压缩这个文件时,它会引发 Bad magic number for file header error
:
这是error
:
..
zip_ref.extractall(destination_to_unzip_file)
File "C:\Python27\lib\zipfile.py", line 1040, in extractall
self.extract(zipinfo, path, pwd)
File "C:\Python27\lib\zipfile.py", line 1028, in extract
return self._extract_member(member, path, pwd)
File "C:\Python27\lib\zipfile.py", line 1082, in _extract_member
with self.open(member, pwd=pwd) as source, \
File "C:\Python27\lib\zipfile.py", line 971, in open
raise BadZipfile("Bad magic number for file header")
zipfile.BadZipfile: Bad magic number for file header
我要解压的文件是这样下载的:
_url = """http://edane.drsr.sk/report/ds_dphs_csv.zip"""
def download_platici_dph(self):
if os.path.isfile(_destination_for_downloads+'platici_dph.zip'):
os.remove(_destination_for_downloads+'platici_dph.zip')
with open(_destination_for_downloads+'platici_dph.zip','w') as f:
response = requests.get(_url,stream=True)
if not response.ok:
print 'Something went wrong'
return False
else:
for block in response.iter_content(1024):
f.write(block)
有人知道问题出在哪里吗?
我尝试在不使用您的 download-code 的情况下下载您的存档,然后使用以下命令解压缩:
import zipfile
with zipfile.ZipFile("ds_dphs_csv.zip") as a:
a.extractall()
它运行良好。 zipfile.BadZipfile 当 header 中出现问题时会引发异常,因此我认为您的文件在下载后已损坏。一定是你的下载方式有问题
您可以在 post 中找到有关异常的更多详细信息:Python - Extracting files from a large (6GB+) zip file
Quoth the documentation for open()
:“打开二进制文件时,您应该将 'b'
附加到模式值以在二进制模式下打开文件”。 =15=]
使用 b
为二进制文件打开输出文件:
with open(_destination_for_downloads+'platici_dph.zip','wb') as f: