吉首自治州住房和城乡建设局网站,什么叫网站建设方案书,张槎网站建设制作,html5导航网站源码下载tarfile — 访问 Tar 压缩文件 
1.概述 
tarfile 模块提供对 Unix tar 存档的读写访问#xff0c;包括压缩文件。除了 POSIX 标准之外#xff0c;还支持几种 GNU tar 扩展。还提供处理 Unix 特殊文件类型#xff0c;如硬链接和软链接#xff0c;以及设备节点. 虽然 tarfile…tarfile — 访问 Tar 压缩文件 
1.概述 
tarfile 模块提供对 Unix tar 存档的读写访问包括压缩文件。除了 POSIX 标准之外还支持几种 GNU tar 扩展。还提供处理 Unix 特殊文件类型如硬链接和软链接以及设备节点. 虽然 tarfile 实现了 Unix 格式但它也可用于在 Microsoft Windows 下创建和读取 tar 存档 
2.测试 Tar 文件 
is_tarfile() 函数返回一个布尔值指示作为参数传递的文件名是否指向有效的 tar 存档。 
import tarfilefor filename in [README.txt, example.tar,bad_example.tar, notthere.tar]:try:print({:15}  {}.format(filename, tarfile.is_tarfile(filename)))except IOError as err:print({:15}  {}.format(filename, err))如果文件不存在is_tarfile() 会引发一个 IOError 
python3 tarfile_is_tarfile.pyREADME.txt  Falseexample.tar  True
bad_example.tar  Falsenotthere.tar  [Errno 2] No such file or directory:
notthere.tar3.tar文件中读取元数据 
直接使用 TarFile 类来操作一个 tar 压缩文件。这个类不仅可用来读取数据而且可用来添加文件到压缩文件中。 
3.1.查看tar包中所有文件名称 
使用 getnames() 来读取压缩文件中所有文件的文件名。 
import tarfilewith tarfile.open(example.tar, r) as t:print(t.getnames())该函数的返回一个字符串列表包含了所有所含文件的文件名 
[index.rst, README.txt]3.2.获取元信息 
除了文件名其他元数据信息可以通过使用 TarInfo 类的实例来获取. 
import tarfile
import timewith tarfile.open(example.tar, r) as t:for member_info in t.getmembers():print(member_info.name)print(  Modified:, time.ctime(member_info.mtime))print(  Mode    :, oct(member_info.mode))print(  Type    :, member_info.type)print(  Size    :, member_info.size, bytes)print()通过 getmembers() 和 getmember() 函数来获取元数据。 
index.rstModified: Fri Aug 19 16:27:54 2016Mode    : 0o644Type    : b0Size    : 9878 bytesREADME.txtModified: Fri Aug 19 16:27:54 2016Mode    : 0o644Type    : b0Size    : 75 bytes 
如果一个所含文件的文件名已知可使用 getmember() 函数获取其所对应的 TarInfo 对象 
import tarfile
import timewith tarfile.open(example.tar, r) as t:for filename in [README.txt, notthere.txt]:try:info  t.getmember(filename)except KeyError:print(ERROR: Did not find {} in tar archive.format(filename))else:print({} is {:d} bytes.format(info.name, info.size))如果文件名所对应的文件不存在压缩文件中函数 getmember() 会抛出一个 KeyError 异常 
README.txt is 75 bytes
ERROR: Did not find notthere.txt in tar archive4.解压文件 
4.1.读取压缩文件内容 
如果需要在程序中的访问某个压缩包中文件的数据可使用 extractfile() 方法并把压缩包里面文件名称作为参数传入。 
import tarfilewith tarfile.open(example.tar, r) as t:# 查看内容的文件列表for filename in [README.txt, notthere.txt]:try:# 返回类型是一个文件可调用操作文件方法读取内容f  t.extractfile(filename)except KeyError:print(ERROR: Did not find {} in tar archive.format(filename))else:print(filename, :)# 查看文件内容print(f.read().decode(utf-8))返回值是一个类似文件的对象从该对象中可以读取归档成员的内容 
README.txt :
The examples for the tarfile module use this file and
example.tar as data.ERROR: Did not find notthere.txt in tar archive 
4.2.压缩包解压成文件 
若想对归档行解包操作并将文件写入文件系统内可以使用 extract() 或 extractall() 取代之前的方法。 
1.解压指定文件 
import tarfile
import osos.mkdir(outdir)
with tarfile.open(example.tar, r) as t:# 指定解压的文件和存储路径t.extract(README.txt, outdir)
print(os.listdir(outdir))归档中的成员被从归档中读取并写入文件系统extract函数中outdir指定解压的路径。 
2.解压所有文件 
标准库文档中有一个注释提到 extractall() 方法的安全性强于 extract() 尤其是在处理不能回滚读取较早时间输入部分的流式数据的情况下所以前者应当更广泛地应用。 extractall() 的第一个参数是文件被写入路径的名称 
import tarfile
import osos.mkdir(outdir)
with tarfile.open(example.tar, r) as t:t.extractall(outdir)
print(os.listdir(outdir))若需从归档中提取特定的文件可将需提取的文件名或者 TarInfo 元数据容器作为参数传递给 extractall() 
import tarfile
import osos.mkdir(outdir)
with tarfile.open(example.tar, r) as t:t.extractall(outdir,members[t.getmember(README.txt)],)
print(os.listdir(outdir))5.创建压缩包 
5.1.创建压缩包 
使用带’w’ 模式的 TarFile 来创建一个新的归档文件 
import tarfileprint(creating archive)
with tarfile.open(tarfile_add.tar, modew) as out:print(adding README.txt)out.add(README.txt)print()
print(Contents:)
with tarfile.open(tarfile_add.tar, moder) as t:for member_info in t.getmembers():print(member_info.name)已有的文件会被清除同时创立一个新的归档文件。使用 add() 函数来添加新成员到新建的归档文件中 
creating archive
adding README.txtContents:
README.txt5.2.重命名文件添加到压缩包 
在创建压缩包时向压缩包添加文件时可以修改文件名称在将文件添加到压缩包。这个可通过构造一个带有 arcname 的 TarInfo 对象并将其传入 addfile() 函数来实现。 
import tarfileprint(creating archive)
with tarfile.open(tarfile_addfile.tar, modew) as out:print(adding README.txt as RENAMED.txt)# 重命名文件名称info  out.gettarinfo(README.txt, arcnameRENAMED.txt)out.addfile(info)print()
print(Contents:)
with tarfile.open(tarfile_addfile.tar, moder) as t:for member_info in t.getmembers():print(member_info.name)该归档只含有一个重命名的文件 
creating archive
adding README.txt as RENAMED.txtContents:
RENAMED.txt5.3.内存数据添加到压缩包 
有时候我们需要直接从内存中将数据写进压缩包而不是先将数据写入文件再将文件添加进压缩包。你可以使用 addfile() 来从类似于打开文件的句柄添加数据来返回字节 
import io
import tarfiletext  This is the data to write to the archive.
data  text.encode(utf-8)with tarfile.open(addfile_string.tar, modew) as out:# 构建一个文件info  tarfile.TarInfo(made_up_file.txt)info.size  len(data)# 将内存中的数据添加到文件并打包out.addfile(info, io.BytesIO(data))print(Contents:)
with tarfile.open(addfile_string.tar, moder) as t:for member_info in t.getmembers():print(member_info.name)f  t.extractfile(member_info)print(f.read().decode(utf-8))通过首先构造 TarInfo 对象 可以为压缩包成员指定你想要的任意名称。在设置了其大小之后使用 addfile() 和 BytesIO 缓冲区作为数据源将数据写进压缩包中。 
Contents:
made_up_file.txt
This is the data to write to the archive.5.4.压缩包追加文件 
除了创建新的归档文件还可以通过设置模式参数为’a’ 来添加新文件到已有的归档文件 
import tarfileprint(creating archive)
with tarfile.open(tarfile_append.tar, modew) as out:out.add(README.txt)print(contents:,)
with tarfile.open(tarfile_append.tar, moder) as t:print([m.name for m in t.getmembers()])print(adding index.rst)
with tarfile.open(tarfile_append.tar, modea) as out:out.add(index.rst)print(contents:,)
with tarfile.open(tarfile_append.tar, moder) as t:print([m.name for m in t.getmembers()])最终的归档文件包含了两个成员 
creating archive
contents:
[README.txt]
adding index.rst
contents:
[README.txt, index.rst]6.处理其他格式压缩文件 
除了正常的 Tar 归档文件tarfile 模块还可处理通过 gzip 或 bzip2 协议压缩的归档文件。要打开一个压缩的归档文件根据不同的压缩协议传入 “:gz” 或 “:bz2” 模式参数到 open() 函数。 
import tarfile
import osfmt  {:30} {:10}
print(fmt.format(FILENAME, SIZE))
print(fmt.format(README.txt, os.stat(README.txt).st_size))FILES  [(tarfile_compression.tar, w),(tarfile_compression.tar.gz, w:gz),(tarfile_compression.tar.bz2, w:bz2),
]for filename, write_mode in FILES:with tarfile.open(filename, modewrite_mode) as out:out.add(README.txt)print(fmt.format(filename, os.stat(filename).st_size),end )print([m.namefor m in tarfile.open(filename, r:*).getmembers()])如果使用 “r:*” 模式读取一个归档文件时tarfile 会自动识别压缩方法。 
FILENAME                       SIZE
README.txt                     75
tarfile_compression.tar        10240      [README.txt]
tarfile_compression.tar.gz     213        [README.txt]
tarfile_compression.tar.bz2    199        [README.txt] 文章转载自: http://www.morning.lkrmp.cn.gov.cn.lkrmp.cn http://www.morning.ynbyk.cn.gov.cn.ynbyk.cn http://www.morning.qhkdt.cn.gov.cn.qhkdt.cn http://www.morning.nwrzf.cn.gov.cn.nwrzf.cn http://www.morning.ttaes.cn.gov.cn.ttaes.cn http://www.morning.krlsz.cn.gov.cn.krlsz.cn http://www.morning.prgnp.cn.gov.cn.prgnp.cn http://www.morning.bhwll.cn.gov.cn.bhwll.cn http://www.morning.zlxkp.cn.gov.cn.zlxkp.cn http://www.morning.nrcbx.cn.gov.cn.nrcbx.cn http://www.morning.ghgck.cn.gov.cn.ghgck.cn http://www.morning.drqrl.cn.gov.cn.drqrl.cn http://www.morning.mdplm.cn.gov.cn.mdplm.cn http://www.morning.rycd.cn.gov.cn.rycd.cn http://www.morning.ymfzd.cn.gov.cn.ymfzd.cn http://www.morning.lftpl.cn.gov.cn.lftpl.cn http://www.morning.rntgy.cn.gov.cn.rntgy.cn http://www.morning.fbdtd.cn.gov.cn.fbdtd.cn http://www.morning.qxmnf.cn.gov.cn.qxmnf.cn http://www.morning.spwm.cn.gov.cn.spwm.cn http://www.morning.jkdtz.cn.gov.cn.jkdtz.cn http://www.morning.qfwzm.cn.gov.cn.qfwzm.cn http://www.morning.hbfqm.cn.gov.cn.hbfqm.cn http://www.morning.mdmqg.cn.gov.cn.mdmqg.cn http://www.morning.ygkq.cn.gov.cn.ygkq.cn http://www.morning.dhqyh.cn.gov.cn.dhqyh.cn http://www.morning.elbae.cn.gov.cn.elbae.cn http://www.morning.ndmh.cn.gov.cn.ndmh.cn http://www.morning.mjtft.cn.gov.cn.mjtft.cn http://www.morning.mnygn.cn.gov.cn.mnygn.cn http://www.morning.cttti.com.gov.cn.cttti.com http://www.morning.mfqmk.cn.gov.cn.mfqmk.cn http://www.morning.rqxch.cn.gov.cn.rqxch.cn http://www.morning.qwbtr.cn.gov.cn.qwbtr.cn http://www.morning.dgxrz.cn.gov.cn.dgxrz.cn http://www.morning.fdhwh.cn.gov.cn.fdhwh.cn http://www.morning.jbfjp.cn.gov.cn.jbfjp.cn http://www.morning.ndlww.cn.gov.cn.ndlww.cn http://www.morning.ykrkb.cn.gov.cn.ykrkb.cn http://www.morning.bpmfz.cn.gov.cn.bpmfz.cn http://www.morning.jyfrz.cn.gov.cn.jyfrz.cn http://www.morning.pyncx.cn.gov.cn.pyncx.cn http://www.morning.mrxqd.cn.gov.cn.mrxqd.cn http://www.morning.nrqnj.cn.gov.cn.nrqnj.cn http://www.morning.lksgz.cn.gov.cn.lksgz.cn http://www.morning.psqs.cn.gov.cn.psqs.cn http://www.morning.qxljc.cn.gov.cn.qxljc.cn http://www.morning.mmplj.cn.gov.cn.mmplj.cn http://www.morning.nnpwg.cn.gov.cn.nnpwg.cn http://www.morning.mzwfw.cn.gov.cn.mzwfw.cn http://www.morning.nrbcx.cn.gov.cn.nrbcx.cn http://www.morning.fengnue.com.gov.cn.fengnue.com http://www.morning.tslxr.cn.gov.cn.tslxr.cn http://www.morning.duqianw.com.gov.cn.duqianw.com http://www.morning.fdfdz.cn.gov.cn.fdfdz.cn http://www.morning.gqwbl.cn.gov.cn.gqwbl.cn http://www.morning.cdlewan.com.gov.cn.cdlewan.com http://www.morning.bmjfp.cn.gov.cn.bmjfp.cn http://www.morning.hpkr.cn.gov.cn.hpkr.cn http://www.morning.wnhgb.cn.gov.cn.wnhgb.cn http://www.morning.tsnmt.cn.gov.cn.tsnmt.cn http://www.morning.hlfnh.cn.gov.cn.hlfnh.cn http://www.morning.nxstj.cn.gov.cn.nxstj.cn http://www.morning.nzlqt.cn.gov.cn.nzlqt.cn http://www.morning.nhpmn.cn.gov.cn.nhpmn.cn http://www.morning.shsh1688.com.gov.cn.shsh1688.com http://www.morning.kfwrq.cn.gov.cn.kfwrq.cn http://www.morning.rtqyy.cn.gov.cn.rtqyy.cn http://www.morning.gfrjs.cn.gov.cn.gfrjs.cn http://www.morning.mgtrc.cn.gov.cn.mgtrc.cn http://www.morning.jpmcb.cn.gov.cn.jpmcb.cn http://www.morning.jzykw.cn.gov.cn.jzykw.cn http://www.morning.fssjw.cn.gov.cn.fssjw.cn http://www.morning.xbtlt.cn.gov.cn.xbtlt.cn http://www.morning.mcjxq.cn.gov.cn.mcjxq.cn http://www.morning.ggnrt.cn.gov.cn.ggnrt.cn http://www.morning.glnmm.cn.gov.cn.glnmm.cn http://www.morning.dnzyx.cn.gov.cn.dnzyx.cn http://www.morning.pjwrl.cn.gov.cn.pjwrl.cn http://www.morning.xmbhc.cn.gov.cn.xmbhc.cn