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 ...
随机推荐
- lambda表达式对比
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- PHP不能创建csv中文名文件
使用iconv 将utf-8转换成gb2312即可$name = iconv("utf-8","gb2312",'分销商下载课件记录');
- php 去除html标记--strip_tags与htmlspecialchars的区别详解
php 去除html标记--strip_tags与htmlspecialchars的区别详解 作者: 字体:[增加 减小] 类型:转载 时间:2013-06-26 本篇文章是对php中去除html ...
- WCF Basic Concept
http://msdn.microsoft.com/library/ee354180.aspx Steps: Designing a Service Contract Implementing a W ...
- MYSQL PASSWORD()
https://www.pythian.com/blog/hashing-algorithm-in-mysql-password-2/ SELECT PASSWORD ("this_is_a ...
- Java中的可变参数以及foreach语句
Java中的可变参数的定义格式如下: 返回值类型 方法名称(类型 ... 参数名称){} foreach语句的格式如下: for ( 数据类型 变量名称 :数据名称){ ... } public ...
- P1083 借教室
思路:前缀和, c表示对于当前的middle, 前缀和 #include <bits/stdc++.h> using namespace std; const int maxn = 1e6 ...
- 文件管理php代码操作文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- os相关方法总结
__file__表示当前文件 os.path.dirname表示当前文件所在路径的父路径 os.pardir表示当前文件名 os.path.join表示合并 os.path.abspath表示绝对路径
- 一种swift编码风格指南(供参考,by linkedin)
http://www.cocoachina.com/swift/20160701/16894.html