python 归档tarfile,zipfile学习
一.tarfile
用法:
tarfile.open(cls, name=None, mode='r', fileobj=None, bufsize=10240, **kwargs) 返回一个TarFile类实例
mode:
'r' or 'r:*' open for reading with transparent compression
'r:' open for reading exclusively uncompressed
'r:gz' open for reading with gzip compression
'r:bz2' open for reading with bzip2 compression
'a' or 'a:' open for appending, creating the file if necessary
'w' or 'w:' open for writing without compression
'w:gz' open for writing with gzip compression
'w:bz2' open for writing with bzip2 compression
'r|*' open a stream of tar blocks with transparent compression
'r|' open an uncompressed stream of tar blocks for reading
'r|gz' open a gzip compressed stream of tar blocks
'r|bz2' open a bzip2 compressed stream of tar blocks
'w|' open an uncompressed stream for writing
'w|gz' open a gzip compressed stream for writing
'w|bz2' open a bzip2 compressed stream for writing
import tarfile
1. 压缩目录
tarball = tarfile.open('/tmp/test.tar.bz2','w:bz2') ##返回TarFile类实例
tarball.add('/root/tar') ##实例方法add
tarball.close() ##与file一样有close方法
2.解压缩tarball
tarfile = tarfile.open('/tmp/test.tar.bz2')
tarfile.extractall() ##解压到同级目录
tarfile.extractall(‘/root’) ##解压到/root目录下
二.zipfile
用法:
zipfile.ZipFile(file,mode,compression=ZIP_STORED,allZip64=False) 返回实例
1.反归档
a = zipfile.ZipFIle('a.zip','r')
a.namelist() #查看归档包含的文件
a.extractall() #
a.close()
2.归档
b= zipfile.ZipFile('b.zip','w')
b.write('a.html') #添加一个文件
b.write('c.txt') #添加另一个
b.namelist() #查看归档包含的文件
b.close()
python 归档tarfile,zipfile学习的更多相关文章
- python 打包下载 zipfile & tarfile
看百度网盘我们会发现这么一个需求,新建一个文件夹,然后向文件夹中上传文件,点击文件夹可以直接下载,下载的是一个压缩文件,将文件夹中所有文件全部打包了下载下来. 在python中,我们要做文件打包下载, ...
- tarfile/zipfile/shutil
当我们选择使用Python来进行Linux系统管理,那么就免不了会在Python代码中对压缩包进行处理,包括创建压缩包.解压.获取压缩包中的文件列表等 tarfile Python的tarfile标准 ...
- python笔记之ZipFile模块
python笔记之ZipFile模块 zipfile模块用来做zip格式编码的压缩和解压缩的,zipfile里有两个非常重要的class, 分别是ZipFile和ZipInfo, 在绝大多数的情况下, ...
- Python框架之Django学习
当前标签: Django Python框架之Django学习笔记(十四) 尛鱼 2014-10-12 13:55 阅读:173 评论:0 Python框架之Django学习笔记(十三) 尛 ...
- Python第十课学习
Python第十课学习 www.cnblogs.com/yuanchenqi/articles/5828233.html 函数: 1 减少代码的重复 2 更易扩展,弹性更强:便于日后文件功能的修改 3 ...
- Python第八课学习
Python第八课学习 www.cnblogs.com/resn/p/5800922.html 1 Ubuntu学习 根 / /: 所有目录都在 /boot : boot配置文件,内核和其他 linu ...
- 第四百一十五节,python常用排序算法学习
第四百一十五节,python常用排序算法学习 常用排序 名称 复杂度 说明 备注 冒泡排序Bubble Sort O(N*N) 将待排序的元素看作是竖着排列的“气泡”,较小的元素比较轻,从而要往上浮 ...
- Python入门到精通学习书籍推荐!
1.Python基础教程(第2版 修订版)<Python基础教程(第2版修订版)>包括Python程序设计的方方面面,内容涉及的范围较广,既能为初学者夯实基础,又能帮助程序员提升技能,适合 ...
- python中confIgparser模块学习
python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...
随机推荐
- HTTP 笔记与总结(8)HTTP 与内容压缩
以环球网的一篇新闻为例,抓包图: (Powered-By-ChinaCache:HIT from 060120b3g7.16 表示当前页面不是来自环球网的主服务器,而是来自中国的缓存服务器节点,HIT ...
- Javascript 笔记与总结(2-15)结构、样式、行为分离
[例] <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- git 使用钩子直接推送到工作目录
远端机器 $ mkdir /www/teacherapi # 创建工作目录 $ cd /data/git $ git init teacherapi.git --bare --shared Init ...
- 归纳整理 PHP大神的十大优良习惯
2015年03月04日11:26 来源:程序师 作者:TECHUG 编辑:覃里 查看全文 赞(0)评论(0) 分享 标签: PHP , 编程指南 , PHP开源项目 [IT168 资讯] 1.多阅读手 ...
- php 文件和表单内容一起上传
<?php $filename = $_POST['filename']; $explain = $_POST['explain']; $upfile = $_FILES['upfile']; ...
- Andrew Ng机器学习公开课笔记 -- Online Learning
网易公开课,第11课 notes,http://cs229.stanford.edu/notes/cs229-notes6.pdf 和之前看到的batch learning算法不一样,batch ...
- quanpailie quanbianli
#include<stdio.h>char data[5]={'a','b','c','d','e'};int vist[5]={0};char step[5]={0};char bu[5 ...
- php数据库两个关联大表的大数组分页处理,防止内存溢出
$ret = self::$db->select($tables, $fields, $where, $bind); if (!empty($ret)) { $retIds = array(); ...
- Git版本控制
官方文档:http://git-scm.com/book/en/v2 github :https://guides.github.com/activities/hello-world/ How to ...
- Android App罕见错误和优化方案
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 1.App如果被定义一个有参数构造函数,那么需要再定义一个无参数的,如果不则会在某些情况下初始化失败 ...