一.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学习的更多相关文章

  1. python 打包下载 zipfile & tarfile

    看百度网盘我们会发现这么一个需求,新建一个文件夹,然后向文件夹中上传文件,点击文件夹可以直接下载,下载的是一个压缩文件,将文件夹中所有文件全部打包了下载下来. 在python中,我们要做文件打包下载, ...

  2. tarfile/zipfile/shutil

    当我们选择使用Python来进行Linux系统管理,那么就免不了会在Python代码中对压缩包进行处理,包括创建压缩包.解压.获取压缩包中的文件列表等 tarfile Python的tarfile标准 ...

  3. python笔记之ZipFile模块

    python笔记之ZipFile模块 zipfile模块用来做zip格式编码的压缩和解压缩的,zipfile里有两个非常重要的class, 分别是ZipFile和ZipInfo, 在绝大多数的情况下, ...

  4. Python框架之Django学习

    当前标签: Django   Python框架之Django学习笔记(十四) 尛鱼 2014-10-12 13:55 阅读:173 评论:0     Python框架之Django学习笔记(十三) 尛 ...

  5. Python第十课学习

    Python第十课学习 www.cnblogs.com/yuanchenqi/articles/5828233.html 函数: 1 减少代码的重复 2 更易扩展,弹性更强:便于日后文件功能的修改 3 ...

  6. Python第八课学习

    Python第八课学习 www.cnblogs.com/resn/p/5800922.html 1 Ubuntu学习 根 / /: 所有目录都在 /boot : boot配置文件,内核和其他 linu ...

  7. 第四百一十五节,python常用排序算法学习

    第四百一十五节,python常用排序算法学习 常用排序 名称 复杂度 说明 备注 冒泡排序Bubble Sort O(N*N) 将待排序的元素看作是竖着排列的“气泡”,较小的元素比较轻,从而要往上浮 ...

  8. Python入门到精通学习书籍推荐!

    1.Python基础教程(第2版 修订版)<Python基础教程(第2版修订版)>包括Python程序设计的方方面面,内容涉及的范围较广,既能为初学者夯实基础,又能帮助程序员提升技能,适合 ...

  9. python中confIgparser模块学习

    python中configparser模块学习 ConfigParser模块在python中用来读取配置文件,配置文件的格式跟windows下的ini配置文件相似,可以包含一个或多个节(section ...

随机推荐

  1. HDR Defered Shading (using MRT)

    http://http.download.nvidia.com/developer/SDK/Individual_Samples/DEMOS/Direct3D9/DeferredShading.zip ...

  2. Using Vertex Texture Displacement for Realistic Water Rendering

    http://blog.csdn.net/soilwork/article/details/709869 Using Vertex Texture Displacement for Realistic ...

  3. Ubuntu 14.04 LTS 安装 VNC Viewer

    1.修改镜像源: /etc/apt/sources.list将"http://archive.ubuntu.com/ubuntu/"替换为: http://cn.archive.u ...

  4. 20145317彭垚 《Java程序设计》第一次实验实验报告

    20145317彭垚 <Java程序设计>第一次实验实验报告 北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1453 指导教师:娄嘉鹏 实验名称:Java开发环境的 ...

  5. the basic index concept

    Computer Science An Overview _J. Glenn Brookshear _11th Edition Over the years numerous variations o ...

  6. Ubuntu apparmor何方神圣

    AppArmor 是一款与SeLinux类似的安全框架/工具,其主要作用是控制应用程序的各种权限,例如对某个目录/文件的读/写,对网络端口的打开/读/写等等. 来之Novell网站的引用: AppAr ...

  7. qTip2 精致的jQuery提示信息插件

    qTip2 精致的jQuery提示信息插件    出处:http://www.cnblogs.com/lwme/archive/2012/02/16/qtip2-jquery-plugin.html ...

  8. AppDelegate

    一.基础知识 1) main.m指定了程序的入口点 UIApplicationMain(argc, argv,nil,NSStringFromClass([StartingPointAppDelega ...

  9. WPF绑定方式

    绑定到其它元素 <Grid>     <StackPanel>         <TextBox x:Name="textbox1" />    ...

  10. 添加事件(jquery)

    对盒子内部的盒子添加跟本身盒子相同的事件的时候,需要小心谨慎一点. 诸如: 从表象上看似乎没有什么太大问题,但是却存在一个致命的问题,就是每次点击box的时候,都会给test添加一个点击事件,而添加的 ...