How does python compress new files to existing ZIP files? How does python compress existing zip files?

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.