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算法输出1-9数组形成的结果为100的所有运算式
Nov 03 Python
PyQt5每天必学之创建窗口居中效果
Apr 19 Python
Django添加favicon.ico图标的示例代码
Aug 07 Python
Python异常处理操作实例详解
Aug 28 Python
使用Python监视指定目录下文件变更的方法
Oct 15 Python
Python实现的IP端口扫描工具类示例
Feb 15 Python
python爬取本站电子书信息并入库的实现代码
Jan 20 Python
python实现拼接图片
Mar 23 Python
python和go语言的区别是什么
Jul 20 Python
python下载的库包存放路径
Jul 27 Python
如何利用pycharm进行代码更新比较
Nov 04 Python
在pyCharm中下载第三方库的方法
Apr 18 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并发对MYSQL造成压力的解决方法
2013/02/21 PHP
thinkphp隐藏index.php/home并允许访问其他模块的实现方法
2016/10/13 PHP
Yii框架学习笔记之session与cookie简单操作示例
2019/04/30 PHP
php开发最强大的IDE编辑的phpstorm 2020.2配置Xdebug调试的详细教程
2020/08/17 PHP
数据结构之利用PHP实现二分搜索树
2020/10/25 PHP
Cookie跨域问题解决方案代码示例
2020/11/24 PHP
菜单效果
2006/10/14 Javascript
给网站上的广告“加速”显示的方法
2007/04/08 Javascript
关于JavaScript的面向对象和继承有利新手学习
2013/01/11 Javascript
Flexigrid在IE下不显示数据的有效处理方法
2014/09/04 Javascript
jQuery动态效果显示人物结构关系图的方法
2015/05/07 Javascript
jQuery实现弹出窗口中切换登录与注册表单
2015/06/05 Javascript
JS调用Android、Ios原生控件
2017/01/06 Javascript
原生js实现类似fullpage的单页/全屏滚动
2017/01/22 Javascript
JS检测window.open打开的窗口是否关闭
2017/06/25 Javascript
javascript 初学教程及五子棋小程序的简单实现
2017/07/04 Javascript
在小程序Canvas中使用measureText的方法示例
2018/10/19 Javascript
深入理解Vue keep-alive及实践总结
2019/08/21 Javascript
中级前端工程师必须要掌握的27个JavaScript 技巧(干货总结)
2019/09/23 Javascript
使用Vue生成动态表单
2019/11/26 Javascript
vue select 获取value和lable操作
2020/08/28 Javascript
[06:04]DOTA2英雄梦之声Vol19卓尔游侠
2014/06/20 DOTA
[06:07]DOTA2-DPC中国联赛 正赛 Ehome vs VG 选手采访
2021/03/11 DOTA
Python  __getattr__与__setattr__使用方法
2008/09/06 Python
python求斐波那契数列示例分享
2014/02/14 Python
python通过ftplib登录到ftp服务器的方法
2015/05/08 Python
python3.5+tesseract+adb实现西瓜视频或头脑王者辅助答题
2018/01/17 Python
解决Tensorflow使用pip安装后没有model目录的问题
2018/06/13 Python
Python绘制K线图之可视化神器pyecharts的使用
2021/03/02 Python
HTML5 语音搜索(淘宝店语音搜素)
2013/01/03 HTML / CSS
英国巧克力贸易公司:Chocolate Trading Company
2017/03/21 全球购物
法国二手手袋、手表和奢侈珠宝购物网站:Collector Square
2018/07/05 全球购物
法制报告会主持词
2014/04/02 职场文书
2014年幼儿园老师工作总结
2014/12/05 职场文书
防汛通知
2015/04/25 职场文书
2015年小学体育工作总结
2015/05/22 职场文书