在python中合并多个zip文件

Posted

技术标签:

【中文标题】在python中合并多个zip文件【英文标题】:merge multiple zip files in python 【发布时间】: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'

【问题讨论】:

【参考方案1】:

好的,我发现了错误 -

我不需要线

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

如果没有这一行,merge_zip_files() 函数现在会获取一个以字节为单位的文件列表,准备传递给 zipfile.ZipFile() 方法,我在上面粘贴的那一行做了两次。

SFS 所有人。

【讨论】:

以上是关于在python中合并多个zip文件的主要内容,如果未能解决你的问题,请参考以下文章

python zip函数的用法

如何使用 python 从位于同一目录中的多个 zip 文件夹中读取 csv 文件?

如何使用 python zipfile 库检查 zip 文件是不是在多个档案中拆分?

如何将所有 python 代码捆绑到一个 zip 文件中?

如何用unzip一次解压多个.zip文件 三种方法

如何读取多个 zip 文件中的所有 csv 文件?