python自动zip压缩目录的方法


Posted in Python onJune 28, 2015

本文实例讲述了python自动zip压缩目录的方法。分享给大家供大家参考。具体实现方法如下:

这段代码来压缩数据库备份文件,没有使用python内置的zip模块,而是使用了zip.exe文件

# Hello, this script is written in Python - http://www.python.org
#
# autozip.py 1.0p
#
# This script will scan a directory (and its subdirectories)
# and automatically zip files (according to their extensions).
#
# This script does not use Python internal ZIP routines.
# InfoZip's ZIP.EXE must be present in the path (InfoZip Dos version 2.3).
# (zip23x.zip at http://www.info-zip.org/pub/infozip/)
#
# Each file will be zipped under the same name (with the .zip extension)
# eg. toto.bak will be zipped to toto.zip
#
# This script is public domain. Feel free to reuse it.
# The author is:
#    Sebastien SAUVAGE
#    <sebsauvage at sebsauvage dot net>
#    http://sebsauvage.net
#
# More quick & dirty scripts are available at http://sebsauvage.net/python/
#
# Directory to scan is hardcoded at the end of the script.
# Extensions to ZIP are hardcoded below:
ext_list = ['.bak','.trn']
import os.path, string
def 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 string.lower(os.path.splitext(fileName)[1]) in ext_list:
      zipMyFile ( os.path.join(directory,fileName) )
def zipMyFile ( fileName ):
  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."

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
Python字符串的encode与decode研究心得乱码问题解决方法
Mar 23 Python
Python Web服务器Tornado使用小结
May 06 Python
tensorflow1.0学习之模型的保存与恢复(Saver)
Apr 23 Python
pytorch 把MNIST数据集转换成图片和txt的方法
May 20 Python
基于Python pip用国内镜像下载的方法
Jun 12 Python
在Python中使用gRPC的方法示例
Aug 08 Python
python 接收处理外带的参数方法
Dec 03 Python
flask框架自定义url转换器操作详解
Jan 25 Python
pytorch查看通道数 维数 尺寸大小方式
May 26 Python
使用python脚本自动生成K8S-YAML的方法示例
Jul 12 Python
python Selenium 库的使用技巧
Oct 16 Python
使用Python实现音频双通道分离
Dec 25 Python
python查找指定具有相同内容文件的方法
Jun 28 #Python
python中getaddrinfo()基本用法实例分析
Jun 28 #Python
python实现搜索指定目录下文件及文件内搜索指定关键词的方法
Jun 28 #Python
分析用Python脚本关闭文件操作的机制
Jun 28 #Python
python实现linux下使用xcopy的方法
Jun 28 #Python
自动化Nginx服务器的反向代理的配置方法
Jun 28 #Python
python读取TXT到数组及列表去重后按原来顺序排序的方法
Jun 26 #Python
You might like
浅析Dos下运行php.exe,出现没有找到php_mbstring.dll 错误的解决方法
2013/06/29 PHP
一个好用的PHP验证码类实例分享
2013/12/27 PHP
php中使用array_filter()函数过滤空数组的实现代码
2014/08/19 PHP
详解PHP中的Traits
2015/07/29 PHP
微信公众号判断用户是否已关注php代码解析
2016/06/24 PHP
Zend Framework入门教程之Zend_Session会话操作详解
2016/12/08 PHP
thinkPHP5.0框架配置格式、加载解析与读取方法
2017/03/17 PHP
php的扩展写法总结
2019/05/14 PHP
javascript 控制弹出窗口
2007/04/10 Javascript
js 颜色选择器(兼容firefox)
2009/03/05 Javascript
jQuery '行 4954 错误: 不支持该属性或方法' 的问题解决方法
2011/01/19 Javascript
使用GruntJS构建Web程序之构建篇
2014/06/04 Javascript
js如何实现淡入淡出效果
2020/11/18 Javascript
js实现砖头在页面拖拉效果
2020/11/20 Javascript
Angular搜索场景中使用rxjs的操作符处理思路
2018/05/30 Javascript
用Python制作检测Linux运行信息的工具的教程
2015/04/01 Python
python使用多进程的实例详解
2018/09/19 Python
Python装饰器限制函数运行时间超时则退出执行
2019/04/09 Python
Python脚本利用adb进行手机控制的方法
2019/07/08 Python
Python定时器线程池原理详解
2020/02/26 Python
Python类的绑定方法和非绑定方法实例解析
2020/03/04 Python
使用Keras实现简单线性回归模型操作
2020/06/12 Python
Python经典五人分鱼实例讲解
2021/01/04 Python
德国内衣、泳装和睡衣网上商店:Bigsize Dessous
2018/07/09 全球购物
初中三年毕业生的自我评价分享
2014/02/14 职场文书
个人房屋买卖协议书(范本)
2014/10/04 职场文书
公司催款律师函
2015/05/27 职场文书
结婚仪式主持词
2015/06/29 职场文书
2016年综治和平安建设宣传月活动总结
2016/04/01 职场文书
普希金的诗歌赏析(3首)
2019/08/20 职场文书
创业计划书之书店
2019/09/10 职场文书
nginx搭建图片服务器的过程详解(root和alias的区别)
2021/03/31 Servers
总结python多进程multiprocessing的相关知识
2021/06/29 Python
详解Spring Boot使用系统参数表提升系统的灵活性
2021/06/30 Java/Android
Python中Matplotlib的点、线形状、颜色以及绘制散点图
2022/04/07 Python
vue生命周期钩子函数以及触发时机
2022/04/26 Vue.js