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程序与C程序的结合使用
Apr 07 Python
Python3读取UTF-8文件及统计文件行数的方法
May 22 Python
Python基于分水岭算法解决走迷宫游戏示例
Sep 26 Python
python抓取网站的图片并下载到本地的方法
May 22 Python
OPENCV去除小连通区域,去除孔洞的实例讲解
Jun 21 Python
Python使用Selenium模块实现模拟浏览器抓取淘宝商品美食信息功能示例
Jul 18 Python
tensorflow实现逻辑回归模型
Sep 08 Python
浅谈pandas用groupby后对层级索引levels的处理方法
Nov 06 Python
Python3.7黑帽编程之病毒篇(基础篇)
Feb 04 Python
使用python创建生成动态链接库dll的方法
May 09 Python
用python监控服务器的cpu,磁盘空间,内存,超过邮件报警
Jan 29 Python
Python编程源码报错解决方法总结经验分享
Oct 05 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的源码中深入了解stdClass类
2014/04/18 PHP
php ci框架中加载css和js文件失败的原因及解决方法
2014/07/29 PHP
ThinkPHP中自定义目录结构的设置方法
2014/08/15 PHP
php实现粘贴截图并完成上传功能
2015/05/17 PHP
thinkphp分页实现效果
2016/10/13 PHP
PHP使用Redis实现Session共享的实现示例
2019/05/12 PHP
js中关于new Object时传参的一些细节分析
2011/03/13 Javascript
JavaScript中的16进制字符(改进)
2011/11/21 Javascript
jQuery插件的写法分享
2013/06/12 Javascript
测试IE浏览器对JavaScript的AngularJS的兼容性
2015/06/19 Javascript
JS验证IP,子网掩码,网关和MAC的方法
2015/07/02 Javascript
javascript实现数组去重的多种方法
2016/03/14 Javascript
BootStrap无限级分类(无限极分类封装版)
2016/08/26 Javascript
详谈Ajax请求中的async:false/true的作用(ajax 在外部调用问题)
2017/02/10 Javascript
bootstrap table表格插件之服务器端分页实例代码
2018/09/12 Javascript
详解vue微信网页授权最终解决方案
2019/06/16 Javascript
javascript 构建模块化开发过程解析
2019/09/11 Javascript
vue el-table实现行内编辑功能
2019/12/11 Javascript
利用Vue的v-for和v-bind实现列表颜色切换
2020/07/17 Javascript
[59:59]EG vs IG 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
用python实现的去除win下文本文件头部BOM的代码
2013/02/10 Python
python求众数问题实例
2014/09/26 Python
Python开发如何在ubuntu 15.10 上配置vim
2016/01/25 Python
Python使用Scrapy保存控制台信息到文本解析
2017/12/27 Python
python入门教程 python入门神图一张
2018/03/05 Python
浅谈Django的缓存机制
2018/08/23 Python
Django单元测试工具test client使用详解
2019/08/02 Python
Python 调用 Windows API COM 新法
2019/08/22 Python
python制作英语翻译小工具代码实例
2019/09/09 Python
Django实现简单网页弹出警告代码
2019/11/15 Python
J2ee常用的设计模式?说明工厂模式
2015/05/21 面试题
清洁工表扬信
2014/01/08 职场文书
中专生毕业个人鉴定
2014/02/26 职场文书
2014最新毕业证代领委托书
2014/09/26 职场文书
2016高考冲刺决心书
2015/09/23 职场文书
Django框架之路由用法
2022/06/10 Python