我想解壓縮我的腳本中的zip文件,使用Python的zipfile
模塊。問題是,當我嘗試解壓縮這個文件,它提出了Bad magic number for file header error
:無法解壓zip文件
這是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)
有誰知道問題在哪裏?
您是否嘗試過使用其他工具來確保解壓縮文件I t是一個有效的ZIP文件? –
@MartinEvans是的,我試過了,它工作。 Rob在回答中說,問題出在下載文件。 –