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.