Python中使用tarfile压缩、解压tar归档文件示例


Posted in Python onApril 05, 2015

Python自带的tarfile模块可以方便读取tar归档文件,牛b的是可以处理使用gzip和bz2压缩归档文件tar.gz和tar.bz2。
与tarfile对应的是zipfile模块,zipfile是处理zip压缩的。请注意:os.system(cmd)可以使Python脚本执行命令,当然包括:tar -czf  *.tar.gz *,tar -xzf *.tar.gz,unzip等,当我觉得这样尽管可以解决问题,但我觉得很业余。

使用tarfile压缩

import tarfile

 

#创建压缩包名

tar = tarfile.open("/tmp/tartest.tar.gz","w:gz")

#创建压缩包

for root,dir,files in os.walk("/tmp/tartest"):

    for file in files:

        fullpath = os.path.join(root,file)

        tar.add(fullpath)

tar.close()

使用tarfile解压
def extract(tar_path, target_path):

    try:

        tar = tarfile.open(tar_path, "r:gz")

        file_names = tar.getnames()

        for file_name in file_names:

            tar.extract(file_name, target_path)

        tar.close()

    except Exception, e:

        raise Exception, e

其中open的原型是:

tarfile.open(name=None, mode='r', fileobj=None, bufsize=10240, **kwargs)

mode的值有:
'r' or 'r:*'   Open for reading with transparent compression (recommended).

'r:'   Open for reading exclusively without compression.

'r:gz'   Open for reading with gzip compression.

'r:bz2'   Open for reading with bzip2 compression.

'a' or 'a:'   Open for appending with no compression. The file is created if it does not exist.

'w' or 'w:'   Open for uncompressed writing.

'w:gz'   Open for gzip compressed writing.

'w:bz2'   Open for bzip2 compressed writing.

更多请参考:tarfile — Read and write tar archive files

Python 相关文章推荐
python模拟enum枚举类型的方法小结
Apr 30 Python
Python自动调用IE打开某个网站的方法
Jun 03 Python
在Python的struct模块中进行数据格式转换的方法
Jun 17 Python
给你选择Python语言实现机器学习算法的三大理由
Nov 15 Python
python使用matplotlib库生成随机漫步图
Aug 27 Python
使用python采集脚本之家电子书资源并自动下载到本地的实例脚本
Oct 23 Python
Python绘图Matplotlib之坐标轴及刻度总结
Jun 28 Python
Django中密码的加密、验密、解密操作
Dec 19 Python
Python使用qrcode二维码库生成二维码方法详解
Feb 17 Python
Python读取JSON数据操作实例解析
May 18 Python
numpy数据类型dtype转换实现
Apr 24 Python
matlab xlabel位置的设置方式
May 21 Python
低版本中Python除法运算小技巧
Apr 05 #Python
Python中使用PDB库调试程序
Apr 05 #Python
使用PDB模式调试Python程序介绍
Apr 05 #Python
python使用calendar输出指定年份全年日历的方法
Apr 04 #Python
python获取指定网页上所有超链接的方法
Apr 04 #Python
python中字典dict常用操作方法实例总结
Apr 04 #Python
python随机生成指定长度密码的方法
Apr 04 #Python
You might like
BBS(php & mysql)完整版(八)
2006/10/09 PHP
我的论坛源代码(八)
2006/10/09 PHP
从康盛产品(discuz)提取出来的模板类
2011/06/28 PHP
php+ajax无刷新上传图片的实现方法
2016/12/06 PHP
关于可运行代码无法正常执行的使用说明
2010/05/13 Javascript
基于Jquery的实现回车键Enter切换焦点
2010/09/14 Javascript
浅析JavaScript中的常用算法与函数
2013/11/21 Javascript
JS按回车键实现登录的方法
2014/08/25 Javascript
JS简单操作select和dropdownlist实例
2014/11/26 Javascript
怎么通过onclick事件获取js函数返回值(代码少)
2015/07/28 Javascript
jQuery中Ajax全局事件引用方式及各个事件(全局/局部)执行顺序
2016/06/02 Javascript
深入理解Node.js的HTTP模块
2016/10/12 Javascript
关于Function中的bind()示例详解
2016/12/02 Javascript
js定时器实现倒计时效果
2017/11/05 Javascript
javascript 通过键名获取键盘的keyCode方法
2017/12/31 Javascript
基于vue.js实现的分页
2018/03/13 Javascript
详解Vue组件插槽的使用以及调用组件内的方法
2018/11/13 Javascript
微信小程序--特定区域滚动到顶部时固定的方法
2019/04/28 Javascript
JavaScript进阶(四)原型与原型链用法实例分析
2020/05/09 Javascript
解决antd 表单设置默认值initialValue后验证失效的问题
2020/11/02 Javascript
Python中的日期时间处理详解
2016/11/17 Python
python实现校园网自动登录的示例讲解
2018/04/22 Python
基于python进行抽样分布描述及实践详解
2019/09/02 Python
使用OpenCV实现道路车辆计数的使用方法
2020/07/15 Python
使用CSS3美化HTML表单的技巧演示
2016/05/17 HTML / CSS
html5 利用canvas手写签名并保存的实现方法
2018/07/12 HTML / CSS
Steiff台湾官网:德国金耳釦泰迪熊
2019/12/26 全球购物
如何用Java实现列出某个目录下的所有子目录
2015/07/20 面试题
服装厂厂长职责
2013/12/16 职场文书
《晏子使楚》教学反思
2014/02/08 职场文书
优秀毕业生自我鉴定
2014/02/11 职场文书
置业顾问岗位职责
2014/03/02 职场文书
2014年驾驶员工作总结
2014/11/18 职场文书
2014年幼儿园老师工作总结
2014/12/05 职场文书
违纪检讨书范文
2015/01/27 职场文书
Python中tqdm的使用和例子
2022/09/23 Python