1. 用zipfile模块打包文件或是目录、解压zip文件

http://wangwei007.blog.51cto.com/68019/1045577

#!/usr/bin/env python
# -*- coding: utf-8 -*- from zipfile import *
import zipfile #解压zip文件
def unzip():
source_zip="c:\\update\\SW_Servers_20120815.zip"
target_dir="c:\\update\\"
myzip=ZipFile(source_zip)
myfilelist=myzip.namelist()
for name in myfilelist:
f_handle=open(target_dir+name,"wb")
f_handle.write(myzip.read(name))
f_handle.close()
myzip.close() #添加文件到已有的zip包中
def addzip():
f = zipfile.ZipFile('archive.zip','w',zipfile.ZIP_DEFLATED)
f.write('file_to_add.py')
f.close() #把整个文件夹内的文件打包
def adddirfile():
f = zipfile.ZipFile('archive.zip','w',zipfile.ZIP_DEFLATED)
startdir = "c:\\mydirectory"
for dirpath, dirnames, filenames in os.walk(startdir):
for filename in filenames:
f.write(os.path.join(dirpath,filename))
f.close()

2. python 调用rar命令行 压缩文件

通过os模块的system()方法调用了系统的rar.exe命令,这个方法会返回一个变量exit_status

import os
import time source = [r'D:\Work\Python\Demo', 'd:\\work\\linux']
target_dir = 'D:\\Work\\backup\\' target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.rar'
zip_command = "rar a %s %s" % (target, ' '.join(source)) if os.system(zip_command) == 0:
print 'Sucessful backup to', target
else:
print 'Backup Failed'

python 压缩 解压缩 文件的更多相关文章

  1. Linux的压缩/解压缩文件处理 zip & unzip

    Linux的压缩/解压缩命令详解及实例 压缩服务器上当前目录的内容为xxx.zip文件 zip -r xxx.zip ./* 解压zip文件到当前目录 unzip filename.zip 另:有些服 ...

  2. 使用VC++压缩解压缩文件夹

    前言   项目中要用到一个压缩解压缩的模块, 看了很多文章和源代码,  都不是很称心, 现在把我自己实现的代码和大家分享. 要求: 1.使用Unicode(支持中文). 2.使用源代码.(不使用静态或 ...

  3. 项目实战工具类(二):ZipUtils(压缩/解压缩文件相关)

    import android.content.Context; import android.util.Log; import java.io.File; import java.io.FileInp ...

  4. Python压缩指定文件及文件夹为zip

    Python压缩指定的文件及文件夹为.zip 代码: def zipDir(dirpath,outFullName): """ 压缩指定文件夹 :param dirpat ...

  5. Python压缩&解压缩

    Python中常用的压缩模块有zipfile.tarfile.gzip 1.zipfile模块的简单使用 import zipfile # 压缩 z1 = zipfile.ZipFile('zip_t ...

  6. 使用 apache ant 轻松实现文件压缩/解压缩(转)

    原文地址:http://blog.csdn.net/irvine007/article/details/6779492 maven配置ant包: <dependency> <grou ...

  7. Linux下的压缩解压缩命令详解

    linux zip命令zip -r myfile.zip ./*将当前目录下的所有文件和文件夹全部压缩成myfile.zip文件,-r表示递归压缩子目录下所有文件. 2.unzipunzip -o - ...

  8. Qt之QuaZIP(zip压缩/解压缩)

    简述 QuaZIP是使用Qt/C++对ZLIB进行简单封装的用于压缩及解压缩ZIP的开源库.适用于多种平台,利用它可以很方便的将单个或多个文件打包为zip文件,且打包后的zip文件可以通过其它工具打开 ...

  9. Linux下的压缩解压缩命令详解及实例

    实例:压缩服务器上当前目录的内容为xxx.zip文件 zip -r xxx.zip ./* 解压zip文件到当前目录 unzip filename.zip ====================== ...

随机推荐

  1. JVM基础02-class文件

    一.class文件结构 介绍之前,请下载一个Bytecode工具,例如byte code viewer或者Java Bytecode Editor,我用的是后者Java Bytecode Editor ...

  2. Linux中kettle连接hadoop并传数据(5)

    http://wiki.pentaho.com/display/BAD/Loading+Data+into+HDFS 新建job

  3. tomcat 7 启动超时设置。。。实在太隐蔽了

    打开Tomcat,选择 Window->Show View->Servers,在主窗口下的窗口中的Servers标签栏鼠标左键双击tomcat服务器名,例如 Tomcat v7.0 Ser ...

  4. varnish屏蔽control+F5导致缓存失效

    刚刚接触Varnish缓存,对静态资源进行缓存.目前问题,当浏览器Control+F5刷新页面,导致缓存失效. 参照:http://zhangxugg-163-com.iteye.com/blog/1 ...

  5. 杭电三部曲一、基本算法;19题 Cow Bowling

    Problem Description The cows don't use actual bowling balls when they go bowling. They each take a n ...

  6. Python in minute

    Python 性能优化相关专题:    https://www.ibm.com/developerworks/cn/linux/l-cn-python-optim/   Python wikipedi ...

  7. YaHoo Web优化的14条法则

    Web应用性能优化黄金法则:先优化前端程序(front-end)的性能,因为这是80%或以上的最终用户响应时间的花费所在. 法则1. 减少HTTP请求次数 80%的最终用户响应时间花在前端程序上,而其 ...

  8. Position & anchorPoint 深入

    引言 相信初接触到CALayer的人都会遇到以下几个问题: 为什么修改anchorPoint会移动layer的位置?CALayer的position点是哪一点呢?anchorPoint与positio ...

  9. 文本输入框和下拉菜单特效-用正则表达式验证E-mail格式

    ———————————————————————————— <script type="text/javascript">                         ...

  10. akka 文章 博客

    http://blog.csdn.net/wsscy2004/article/category/2430395 Actor生命周期理解 Actor生命周期理解 镇图:Actor内功心法图 Actor的 ...