How does python compress new files to existing ZIP files? How does python compress existing zip files?
This article shares the code for compressing new python files to existing ZIP files for your reference. The details are as follows:
The main point is to use the Python standard library zipfile to create a compressed file. If you use the 'A' mode, you can append new content.
From zipfile import ZipFilefrom OS import listdirfrom OS. path import isfile, isdir, joindef addfile1_zipfile (srcDir, fp): # traverse all files in this folder for subpath in listdir (srcDir): subpath = join (srcDir, subpath) if isfile (subpath): # compress the file directly to the ZIP file fp. write (subpath) elif isdir (subpath): # write the name of the subfolder first if it is a subfolder. # Call the function recursively # compress all files into the ZIP file fp. write (subpath) addfile1_zipfile (subpath, fp) def zipCompress (srcDir, desZipfile): with ZipFile (desZipfile, mode = 'A') as fp: addfile1_zipfile (srcDir, fp) # test function paths = [r 'C: \ python34 \ Scripts ', r 'C: \ python34 \ Dlls', r 'C: \ eclipse '] for path in paths: zipCompress (path, 'test.zip ')
Source: python hut
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.