python tarfile模块
TarFile类对于就是tar压缩包实例. 其由member块组成, member块则包括header块和data块. 每个member以TarInfo对象形式描述. 所以TarFile就是TarInfo的序列. 其初始化函数的形参和TarFile类的属性对应, 其中比较重要是dereference属性,默认False(此时symbolic文件会以符号文件存进去),设置为True时会将其目标文件存到压缩包.
- getmember(name), getmembers(), getnames(): 分别返回指定文件名的TarInfo, 所有TarInfo和所有TarInfo文件名. 后两者顺序一致. 如果里面有很多个同名的member, 则取最后的.
- list(verbose=True) 列出tar里的文件信息, verbose相对于
ls -l - next() 就是for方法迭代器调用的, 返回下一个TarInfo对象.
- extractall(path=”.”, members=None), 解压所有文件, path是解压路径,默认当前目录,如果指明members(一个子集,TarInfo列表, 也可以是迭代器).
- extract(member, path=””), 解压指定member的文件,path是解压路径.
- extractfile(member): 提取相应对象为一个只读文件对象. member这里可以是文件名或者TarInfo.
- add(name, arcname=None, recursive=True, exclude=None, filter=None): 根据文件名创建TarInfo对象并添加文件到压缩包,可以指定arcname在压缩包里面使用的另外的名字, recursive是对文件夹处理时是否递归, exclude不建议用,filter(需要用key=value形式)是一个函数名,输入是TarInfo对象, 返回新的TarInfo对象或None(None的话就不被写入到压缩包, 可以用于过滤, 所以替代了exclude)
- addfile(tarinfo, fileobj=None): 将TarInfo对象或者文件对象添加到压缩包.一般配合gettarinfo使用
- gettarinfo(name=None, arcname=None, fileobj=None): 通过文件名或文件对象来创造TarInfo对象. arcname可以重命名文件
import tarfile # 压缩
tar = tarfile.open('your.tar','w') # 创建一个压缩包
tar.add('/Users/wupeiqi/PycharmProjects/bbs2.log', arcname='bbs2.log') # 将文件添加到压缩包并命名
tar.add('/Users/wupeiqi/PycharmProjects/cmdb.log', arcname='cmdb.log') #
tar.close() # 关闭压缩包 # 解压
tar = tarfile.open('your.tar','r') # 打开一个压缩包
tar.extractall() # 解压包内所有文件(可设置解压地址)
tar.close() # 关闭压缩包
压缩某个目录下所有文件
def compress_file(tarfilename, dirname): # tarfilename是压缩包名字,dirname是要打包的目录
if os.path.isfile(dirname):
with tarfile.open(tarfilename, 'w') as tar:
tar.add(dirname)
else:
with tarfile.open(tarfilename, 'w') as tar:
for root, dirs, files in os.walk(dirname):
for single_file in files:
# if single_file != tarfilename:
filepath = os.path.join(root, single_file)
tar.add(filepath) compress_file('test.tar', 'test.txt')
compress_file('t.tar', '.')
添加文件到已有的tar包中
def addfile(tarfilename, dirname): # tarfilename是压缩包名字,dirname是要打包的目录
if os.path.isfile(dirname):
with tarfile.open(tarfilename, 'a') as tar:
tar.add(dirname)
else:
with tarfile.open(tarfilename, 'a') as tar:
for root, dirs, files in os.walk(dirname):
for single_file in files:
# if single_file != tarfilename:
filepath = os.path.join(root, single_file)
tar.add(filepath) addfile('t.tar', 'ttt.txt')
addfile('t.tar', 'ttt')
其中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.
python tarfile模块的更多相关文章
- Python tarfile模块解压报错 invalid mode ('wb') or filename
问题原因 在使用tarfile模块解压一份Linux服务器上的打包文件时, 出现了错误提示: IOError: [Errno 22] invalid mode ('wb') or filename. ...
- python tarfile模块基本使用
1.压缩一个文件夹下的所有文件 #coding=utf8 import os import tarfile __author__ = 'Administrator' def main(): cwd = ...
- python tarfile模块打压缩包,arcname的用法
D:\szh\noses文件夹下有子文件夹和文件 with tarfile.open('E:\\szh.tar', "w") as tar: tar.add('D:\\ ...
- Python第二十天 shutil 模块 zipfile tarfile 模块
Python第二十天 shutil 模块 zipfile tarfile 模块 os文件的操作还应该包含移动 复制 打包 压缩 解压等操作,这些os模块都没有提供 shutil 模块shut ...
- Python中模块之shutil及zipfile&tarfile的功能介绍
shutil的功能介绍及其他打包.压缩模块 1. shutil模块的方法 chown 更改指定路径的属组 2. copy 拷贝文件和权限 方法:shutil.copy(src,dst,*,follow ...
- python标准模块(二)
本文会涉及到的模块: json.pickle urllib.Requests xml.etree configparser shutil.zipfile.tarfile 1. json & p ...
- Day05 - Python 常用模块
1. 模块简介 模块就是一个保存了 Python 代码的文件.模块能定义函数,类和变量.模块里也能包含可执行的代码. 模块也是 Python 对象,具有随机的名字属性用来绑定或引用. 下例是个简单的模 ...
- Day5 模块及Python常用模块
模块概述 定义:模块,用一砣代码实现了某类功能的代码集合. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,提供了代码的重用性.在Python中,一个.py文件就称之为一个模块(Mod ...
- Python ftplib模块
Python ftplib模块 官方文档:https://docs.python.org/3/library/ftplib.html?highlight=ftplib#module-ftplib 实例 ...
随机推荐
- libev+TCP服务器事件轮询实例demo
#include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> #include <std ...
- 题解 [BZOJ1925][SDOI2010] 地精部落
题面 解析 这个似乎并不好讲啊 设\(f[i][j]\)表示有\(i\)座山, 最后一座山到达高度是\(i\)座中第\(j\)大的, 且最后一座山是山谷. 注意,\(i\)是代表有\(i\)座山,并不 ...
- 【leetcode】1291. Sequential Digits
题目如下: An integer has sequential digits if and only if each digit in the number is one more than the ...
- 开源笔记软件Joplin
Joplin is a free, open source note taking and to-do application, which can handle a large number of ...
- (九)文档和视图,Invalidate,数据库编程
一.文档视图结构 文档类(CDocument):存储加载(读写)数据视图类(CView):显示和修改数据 1)单文档 a)文档模板:把框架窗口.文档.视图关联在一起b)文档类(CDocument): ...
- 【CUDA 基础】5.1 CUDA共享内存概述
title: [CUDA 基础]5.1 CUDA共享内存概述 categories: - CUDA - Freshman tags: - CUDA共享内存模型 - CUDA共享内存分配 - CUDA共 ...
- 2016 Multi-University Training Contest 2 部分题解
1009,直接贪心,只要让后面的尽量小,第一位和第二位尽量大即可. 1011,直接统计奇数的字母的个数,然后用偶数的个数平均分配到它们上面即可.代码如下: #include <stdio.h&g ...
- BUUCTF平台-web-边刷边记录-2
1.one line tool <?php if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $_SERVER['REMOTE_ADDR'] = $_ ...
- How to delete System Profiles for those registered with Red Hat Subscription Management (RHSM)?
Environment Red Hat Customer Portal Certificate Based Subscription Red Hat Subscription Management ( ...
- OpenWrt下如何配置网络?
答: 使用uci进行配置,示例如下: uci get network.wan.ifname (笔者得到eth1) uci set network.wan.ifname=ethx (如: uci set ...