Merge multiple zip files into a single zip file in Python

前端 未结 2 2138
暗喜
暗喜 2021-02-19 05:42

I have multiple zip files that have the same structure -- they contain XML files at the root level. All files in each zip file are unique (no duplicates across the zip files). I

2条回答
  •  独厮守ぢ
    2021-02-19 06:25

    This is the shortest version I could come up with:

    >>> import zipfile as z
    >>> z1 = z.ZipFile('z1.zip', 'a')
    >>> z2 = z.ZipFile('z2.zip', 'r')
    >>> z1.namelist()
    ['a.xml', 'b.xml']
    >>> z2.namelist()
    ['c.xml', 'd.xml']
    >>> [z1.writestr(t[0], t[1].read()) for t in ((n, z2.open(n)) for n in z2.namelist())]
    [None, None]
    >>> z1.namelist()
    ['a.xml', 'b.xml', 'c.xml', 'd.xml']
    >>> z1.close()
    

    Without testing the alternative, to me this is the best (and probably most obvious too!) solution because - assuming both zip files contains the same amount of data, this method requires the decompression and re-compression of only half of it (1 file).

    PS: List comprehension is there just to keep instructions on one line in the console (which speeds debugging up). Good pythonic code would require a proper for loop, given that the resulting list serves no purpose...

    HTH!

提交回复
热议问题