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实现的数据结构与算法之快速排序详解
Apr 22 Python
对pandas的dataframe绘图并保存的实现方法
Aug 05 Python
Python科学画图代码分享
Nov 29 Python
Python SqlAlchemy动态添加数据表字段实例解析
Feb 07 Python
python中使用print输出中文的方法
Jul 16 Python
详解Python中pandas的安装操作说明(傻瓜版)
Apr 08 Python
TensorFlow Saver:保存和读取模型参数.ckpt实例
Feb 10 Python
logging level级别介绍
Feb 21 Python
解决Python Matplotlib绘图数据点位置错乱问题
May 16 Python
Python3 requests模块如何模仿浏览器及代理
Jun 15 Python
如何用python插入独创性声明
Mar 31 Python
Python echarts实现数据可视化实例详解
Mar 03 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
php 计算两个时间戳相隔的时间的函数(小时)
2009/12/18 PHP
7个超级实用的PHP代码片段
2011/07/11 PHP
php类常量的使用详解
2013/06/08 PHP
PHP实现取得HTTP请求的原文
2014/08/18 PHP
PHP一致性hash分布式算法封装类定义与用法示例
2018/08/04 PHP
JS面向对象编程之对象使用分析
2010/08/19 Javascript
window.name代替cookie的实现代码
2010/11/28 Javascript
jquery显示隐藏input对象
2014/07/21 Javascript
jQuery实现自定义checkbox和radio样式
2015/07/13 Javascript
了解VUE的render函数的使用
2017/06/08 Javascript
vue父组件点击触发子组件事件的实例讲解
2018/02/08 Javascript
详解基于Vue2.0实现的移动端弹窗(Alert, Confirm, Toast)组件
2018/08/02 Javascript
angularJs自定义过滤器实现手机号信息隐藏的方法
2018/10/08 Javascript
javascript中call()、apply()的区别
2019/03/21 Javascript
我要点爆”微信小程序云开发之项目建立与我的页面功能实现
2019/05/26 Javascript
微信小程序8种数据通信的方式小结
2020/02/03 Javascript
Vue使用轮询定时发送请求代码
2020/08/10 Javascript
Django中利用filter与simple_tag为前端自定义函数的实现方法
2017/06/15 Python
Python实现曲线点抽稀算法的示例
2017/10/12 Python
基于python if 判断选择结构的实例详解
2019/05/06 Python
Python中的引用知识点总结
2019/05/20 Python
Python批量查询关键词微信指数实例方法
2019/06/27 Python
基于python 等频分箱qcut问题的解决
2020/03/03 Python
CSS3属性box-shadow使用指南
2014/12/09 HTML / CSS
html5-Canvas可以在web中绘制各种图形
2012/12/26 HTML / CSS
HTML5 新表单类型示例代码
2018/03/20 HTML / CSS
Giglio俄罗斯奢侈品购物网:男士、女士、儿童高级时装
2018/07/27 全球购物
C#中有没有静态构造函数,如果有是做什么用的?
2016/06/04 面试题
软件工程师面试题
2012/06/25 面试题
口头翻译求职人自荐信
2013/12/07 职场文书
酒店工程部主管岗位职责
2015/04/16 职场文书
信访维稳承诺书
2015/05/04 职场文书
mysql升级到5.7时,wordpress导数据报错1067的问题
2021/05/27 MySQL
梳理总结Python开发中需要摒弃的18个坏习惯
2022/01/22 Python
vue二维数组循环嵌套方式 循环数组、循环嵌套数组
2022/04/24 Vue.js
SQL Server中的游标介绍
2022/05/20 SQL Server