Python automatic Zip compression directory method

Source: Internet
Author: User
Tags dot net zip extension
The example in this article describes the Python automatic Zip compression directory method. Share to everyone for your reference. The implementation method is as follows:

This code compresses the database backup file, does not use the Python built-in zip module, but uses the Zip.exe file

# Hello, this script was written in python-http://www.python.org## autozip.py 1.0p## This script would scan a directory (a nd it subdirectories) # and automatically zip files (according to their extensions). # # This script does not use Python int ernal zip routines.# infozip ' s zip. EXE must is present in the path (Infozip Dos version 2.3). # (Zip23x.zip at http://www.info-zip.org/pub/infozip/) # each FI Le'll be zipped under the same name (with the. zip extension) # eg. Toto.bak'll be zipped to toto.zip## this script was public domain. Feel free to reuse it.# the author is:# Sebastien sauvage#
 
  
   # http://sebsauvage.net## More Quick & dirty scripts is available at http://sebsauvage.net/python/## Directo Ry to scan was hardcoded at the end of the script.# Extensions to ZIP be hardcoded below:ext_list = ['. Bak ', '. trn ']import Os.path, Stringdef autozip (directory): Os.path.walk (Directory,walk_callback, ') def walk_callback (Args,directory, files): print ' scanning ', directory for FileName in Files:if os.path.isfile (Os.path.join (directory,filename)) and Str Ing.lower (Os.path.splitext (FileName) [1]) in Ext_list:zipmyfile (Os.path.join (directory,filename)) def zipmyfile (f  Ilename): Os.chdir (os.path.dirname (filename)) Zipfilename = Os.path.splitext (os.path.basename (filename)) [0]+]. zip  print ' zipping to ' + zipfilename Os.system (' zip-mj9 "' +zipfilename+ '" "" ' +filename+ ' "') Autozip (R ' C:\mydirectory ') print "All done."
  
 

Hopefully this article will help you with Python programming.

  • 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.