[Python] python自动zip压缩目录 →→→→→进入此内容的聊天室

来自 , 2020-09-15, 写在 Python, 查看 164 次.
URL http://www.code666.cn/view/8819159f
  1. # Hello, this script is written in Python - http://www.python.org
  2. #
  3. # autozip.py 1.0p
  4. #
  5. # This script will scan a directory (and its subdirectories)
  6. # and automatically zip files (according to their extensions).
  7. #
  8. # This script does not use Python internal ZIP routines.
  9. # InfoZip's ZIP.EXE must be present in the path (InfoZip Dos version 2.3).
  10. # (zip23x.zip at http://www.info-zip.org/pub/infozip/)
  11. #
  12. # Each file will be zipped under the same name (with the .zip extension)
  13. # eg. toto.bak will be zipped to toto.zip
  14. #
  15. # This script is public domain. Feel free to reuse it.
  16. # The author is:
  17. #       Sebastien SAUVAGE
  18. #       <sebsauvage at sebsauvage dot net>
  19. #       http://sebsauvage.net
  20. #
  21. # More quick & dirty scripts are available at http://sebsauvage.net/python/
  22. #
  23. # Directory to scan is hardcoded at the end of the script.
  24. # Extensions to ZIP are hardcoded below:
  25. ext_list = ['.bak','.trn']
  26.  
  27. import os.path, string
  28.  
  29. def autozip( directory ):
  30.     os.path.walk(directory,walk_callback,'')
  31.  
  32. def walk_callback(args,directory,files):
  33.     print 'Scanning',directory
  34.     for fileName in files:
  35.         if os.path.isfile(os.path.join(directory,fileName)) and string.lower(os.path.splitext(fileName)[1]) in ext_list:
  36.             zipMyFile ( os.path.join(directory,fileName) )
  37.  
  38. def zipMyFile ( fileName ):
  39.     os.chdir( os.path.dirname(fileName) )
  40.     zipFilename = os.path.splitext(os.path.basename(fileName))[0]+".zip"
  41.     print '  Zipping to '+ zipFilename
  42.     os.system('zip -mj9 "'+zipFilename+'" "'+fileName+'"')
  43.  
  44. autozip( r'C:\mydirectory' )
  45. print "All done."
  46. #//python/5175

回复 "python自动zip压缩目录"

这儿你可以回复上面这条便签

captcha