在python中合并多个zip文件

问题描述

我知道有两个类似的问题,实际上我是从这段代码获取的,但是我不明白为什么会出现错误

我有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'

解决方法

好的,我发现了错误-

我不需要排队

zipfiles_objects = [zipfile.ZipFile(fbytes) for fbytes in filebytes]

在没有这一行的情况下,merge_zip_files()函数现在获取准备好传递给zipfile.ZipFile()方法的字节列表的文件,我在上面粘贴的那一行中做了两次。

SFS大家。