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 ...
随机推荐
- NV OIT algorithm : Depth peeling is a fragment-level depth sorting technique
https://developer.nvidia.com/content/interactive-order-independent-transparency Correctly rendering ...
- spring mvc和spring配置扫描包问题
spring mvc和spring俩配置文件,其中都要配置扫描包. <context:component-scan base-package="com.controller" ...
- [转]常用 Git 命令清单
作者: 阮一峰 我每天使用 Git ,但是很多命令记不住. 一般来说,日常使用只要记住下图6个命令,就可以了.但是熟练使用,恐怕要记住60-100个命令. 下面是我整理的常用 Git 命令清单.几个专 ...
- Worker 工作 后台js 工作
<script type="text/javascript"> var w; function startWorker() { if (typeof (Worker) ...
- iOS中利用CoreTelephony获取用户当前网络状态(判断2G,3G,4G)
前言: 在项目开发当中,往往需要利用网络.而用户的网络环境也需要我们开发者去注意,根据不同的网络状态作相应的优化,以提升用户体验. 但通常我们只会判断用户是在WIFI还是移动数据,而实际上,移动数据也 ...
- 4.PHP内核探索:单进程SAPI生命周期
CLI/CGI模式的PHP属于单进程的SAPI模式.这类的请求在处理一次请求后就关闭.也就是只会经过如下几个环节: 开始 - 请求开始 - 请求关闭 - 结束 SAPI接口实现就完成了其生命周期. 单 ...
- delimiter
http://www.mysqltutorial.org/getting-started-with-mysql-stored-procedures.aspx The first command is ...
- 回调的代理(delegate)实现
1.CoreManage.h #import <Foundation/Foundation.h> @protocol SampleProtocol; //声明核心类的属性和方法 @inte ...
- Struts Upload上传文件
1.Unable to find 'struts.multipart.saveDir' property setting. Defaulting to javax.servlet.context.te ...
- Python 扫面文件夹中的文件
#coding=utf-8 import os,sys reload(sys) sys.setdefaultencoding("utf-8") def scan_files_sub ...