【发布时间】:2020-09-03 12:14:00
【问题描述】:
我知道有几个类似的问题,我实际上从中提取了一些代码,但我不明白为什么会出现错误。
我有 3 个网址,每个网址都给我一个 zip 文件,我想使用 python 将它们合并为 1 个而不保存到光盘。
import zipfile
import io
import requests
from requests.exceptions import HTTPError
EA = 'https://fews.net/data_portal_download/download?data_file_path=http%3A//' \
'shapefiles.fews.net.s3.amazonaws.com/HFIC/EA/east-africa202006.zip'
SA = 'https://fews.net/data_portal_download/download?data_file_path=http%3A//' \
'shapefiles.fews.net.s3.amazonaws.com/HFIC/SA/southern-africa202006.zip'
WA = 'https://fews.net/data_portal_download/download?data_file_path=http%3A//' \
'shapefiles.fews.net.s3.amazonaws.com/HFIC/WA/west-africa202006.zip'
urls = [EA, SA, WA]
# supporting funcs
def get_zip(url):
try:
response = requests.get(url)
# If the response was successful, no Exception will be raised
response.raise_for_status()
except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}')
except Exception as err:
print(f'Other error occurred: {err}')
else:
return response
def merge_zip_files(zipfiles):
with zipfile.ZipFile(zipfiles[0], 'a') as first_zipfile:
for filename in zipfiles[1:]:
other_zipfile = zipfile.ZipFile(filename, 'r')
for n in other_zipfile.namelist():
first_zipfile.writestr(n, other_zipfile.open(n).read())
return first_zipfile
# extract and merge zip files
zipfiles = [get_zip(url).content for url in urls]
filebytes = [io.BytesIO(zfile) for zfile in zipfiles]
zipfiles_objects = [zipfile.ZipFile(fbytes) for fbytes in filebytes]
# til here no error
merged_zipfile = merge_zip_files(zipfiles=zipfiles)
此时我收到此错误:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/zipfile.py", line 1287, in __init__
self._RealGetContents()
File "/usr/local/lib/python3.8/zipfile.py", line 1330, in _RealGetContents
endrec = _EndRecData(fp)
File "/usr/local/lib/python3.8/zipfile.py", line 262, in _EndRecData
fpin.seek(0, 2)
AttributeError: 'ZipFile' object has no attribute 'seek'
【问题讨论】:
标签: python-3.x file merge zipfile