• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python自动zip压缩目录的方法

python自动zip压缩目录的方法

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python中zip函数,python zipfile,python中zip,python zip,python3 zip等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了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."

</div>

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

</div>

您可能想查找下面的文章:

  • Python用zip函数同时遍历多个迭代器示例详解
  • Python压缩解压缩zip文件及破解zip文件密码的方法
  • python查看zip包中文件及大小的方法
  • python自动zip压缩目录的方法
  • python压缩文件夹内所有文件为zip文件的方法
  • Python遍历zip文件输出名称时出现乱码问题的解决方法
  • Python遍历zip文件输出名称时出现乱码问题的解决方法
  • Python中zip()函数用法实例教程

相关文章

  • python使用any判断一个对象是否为空的方法
  • 使用Python的Scrapy框架十分钟爬取美女图
  • Java Web开发过程中登陆模块的验证码的实现方式总结
  • python进阶教程之模块(module)介绍
  • 详解Python的Twisted框架中reactor事件管理器的用法
  • Python获取文件所在目录和文件名的方法
  • python登录pop3邮件服务器接收邮件的方法
  • Pycharm学习教程(6) Pycharm作为Vim编辑器使用
  • Python selenium 父子、兄弟、相邻节点定位方式详解
  • Linux 发邮件磁盘空间监控(python)

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python网络编程学习笔记(九):数据库客户端 DB-API
    • python读文件逐行处理的示例代码分享
    • tensorflow如何继续训练之前保存的模型实例
    • Python远程桌面协议RDPY安装使用介绍
    • 使用Python编写简单的端口扫描器的实例分享
    • python修改字典内key对应值的方法
    • 使用C#配合ArcGIS Engine进行地理信息系统开发
    • Python用list或dict字段模式读取文件的方法
    • Python3.2中Print函数用法实例详解
    • 在Python中使用dict和set方法的教程

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有