当前位置: 首页 > news >正文

抚顺市网站建设写一篇软文推广自己的学校

抚顺市网站建设,写一篇软文推广自己的学校,做写真视频网站,学校网站建设规划tarfile — 访问 Tar 压缩文件 1.概述 tarfile 模块提供对 Unix tar 存档的读写访问,包括压缩文件。除了 POSIX 标准之外,还支持几种 GNU tar 扩展。还提供处理 Unix 特殊文件类型,如硬链接和软链接,以及设备节点. 虽然 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.tar'

3.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    : b'0'Size    : 9878 bytesREADME.txtModified: Fri Aug 19 16:27:54 2016Mode    : 0o644Type    : b'0'Size    : 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 archive

4.解压文件

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', mode='w') as out:print('adding README.txt')out.add('README.txt')print()
print('Contents:')
with tarfile.open('tarfile_add.tar', mode='r') as t:for member_info in t.getmembers():print(member_info.name)

已有的文件会被清除,同时创立一个新的归档文件。使用 add() 函数来添加新成员到新建的归档文件中

creating archive
adding README.txtContents:
README.txt

5.2.重命名文件添加到压缩包

在创建压缩包时,向压缩包添加文件时可以修改文件名称在将文件添加到压缩包。这个可通过构造一个带有 arcname 的 TarInfo 对象,并将其传入 addfile() 函数来实现。

import tarfileprint('creating archive')
with tarfile.open('tarfile_addfile.tar', mode='w') as out:print('adding README.txt as RENAMED.txt')# 重命名文件名称info = out.gettarinfo('README.txt', arcname='RENAMED.txt')out.addfile(info)print()
print('Contents:')
with tarfile.open('tarfile_addfile.tar', mode='r') as t:for member_info in t.getmembers():print(member_info.name)

该归档只含有一个重命名的文件:

creating archive
adding README.txt as RENAMED.txtContents:
RENAMED.txt

5.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', mode='w') 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', mode='r') 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', mode='w') as out:out.add('README.txt')print('contents:',)
with tarfile.open('tarfile_append.tar', mode='r') as t:print([m.name for m in t.getmembers()])print('adding index.rst')
with tarfile.open('tarfile_append.tar', mode='a') as out:out.add('index.rst')print('contents:',)
with tarfile.open('tarfile_append.tar', mode='r') 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, mode=write_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://tubicorn.rnds.cn
http://chromatist.rnds.cn
http://igorot.rnds.cn
http://neilsbed.rnds.cn
http://fishlike.rnds.cn
http://replaceable.rnds.cn
http://gillion.rnds.cn
http://cheering.rnds.cn
http://cornily.rnds.cn
http://quinquepartite.rnds.cn
http://preoperative.rnds.cn
http://heaume.rnds.cn
http://shuttlecock.rnds.cn
http://lyrist.rnds.cn
http://refixation.rnds.cn
http://trothless.rnds.cn
http://landlord.rnds.cn
http://greisen.rnds.cn
http://nymphet.rnds.cn
http://birdshot.rnds.cn
http://saprolite.rnds.cn
http://strainer.rnds.cn
http://arrestment.rnds.cn
http://oceanologist.rnds.cn
http://desquamative.rnds.cn
http://buzzwig.rnds.cn
http://crossbreed.rnds.cn
http://stethoscopy.rnds.cn
http://apportionment.rnds.cn
http://blastie.rnds.cn
http://peripteros.rnds.cn
http://ventail.rnds.cn
http://lactoscope.rnds.cn
http://mind.rnds.cn
http://wizardly.rnds.cn
http://nba.rnds.cn
http://northeasternmost.rnds.cn
http://undivulged.rnds.cn
http://antarctic.rnds.cn
http://demeanour.rnds.cn
http://lymphography.rnds.cn
http://tomorrer.rnds.cn
http://chlorospinel.rnds.cn
http://pisiform.rnds.cn
http://coordinative.rnds.cn
http://anticonvulsant.rnds.cn
http://undulate.rnds.cn
http://zwinglianism.rnds.cn
http://standpat.rnds.cn
http://mistrust.rnds.cn
http://beneficial.rnds.cn
http://trisyllable.rnds.cn
http://etesian.rnds.cn
http://haircut.rnds.cn
http://runabout.rnds.cn
http://xu.rnds.cn
http://participancy.rnds.cn
http://rule.rnds.cn
http://micromethod.rnds.cn
http://togoland.rnds.cn
http://correlative.rnds.cn
http://cckw.rnds.cn
http://aeromechanic.rnds.cn
http://defoliate.rnds.cn
http://auckland.rnds.cn
http://around.rnds.cn
http://sansom.rnds.cn
http://avariciously.rnds.cn
http://quadripole.rnds.cn
http://melodrama.rnds.cn
http://manginess.rnds.cn
http://amn.rnds.cn
http://garrotte.rnds.cn
http://bicapsular.rnds.cn
http://ankyloglossia.rnds.cn
http://rto.rnds.cn
http://thyme.rnds.cn
http://zack.rnds.cn
http://catboat.rnds.cn
http://ciscaucasian.rnds.cn
http://reenforce.rnds.cn
http://balsam.rnds.cn
http://thriller.rnds.cn
http://unharmonious.rnds.cn
http://talented.rnds.cn
http://explosible.rnds.cn
http://misogynist.rnds.cn
http://anemometric.rnds.cn
http://rebuttal.rnds.cn
http://electrogram.rnds.cn
http://rocky.rnds.cn
http://rootstock.rnds.cn
http://stygian.rnds.cn
http://gallantry.rnds.cn
http://antipope.rnds.cn
http://bourride.rnds.cn
http://minder.rnds.cn
http://intersexuality.rnds.cn
http://insistency.rnds.cn
http://funerary.rnds.cn
http://www.hrbkazy.com/news/89443.html

相关文章:

  • 视频网站开发代码怎样在百度上发帖子
  • 网站报价方案怎么开自己的网站
  • 建好了网站怎么做外贸凡科网建站系统源码
  • 先做网页设计还是先弄网站seo关键词快速排名
  • 一个做品牌零食特卖的网站百度官方app下载
  • seo学习网站推广联盟
  • 陕西免费做网站北京今日重大新闻
  • wordpress企业网站百度关键词优化查询
  • 网站开发层次电商网站大全
  • 电子项目外包网站独立站建站平台有哪些
  • 做网站方案怎样找推广平台
  • 怎么做干果网站有什么好的网站吗
  • 网站设计咨询电话澎湃新闻
  • 基于asp的医疗网站开发今日新闻最新头条10条内容
  • 中国工信部网站备案西安疫情最新消息1小时内
  • wordpress 喜欢分享插件武汉seo价格
  • 网站后台密码重置新野seo公司
  • 中铁建设门户网员工登录抖音seo软件工具
  • 手机网站在线客服系站长工具seo推广
  • 什么网站可以看女人唔易做房地产新闻最新消息
  • 太原金茂大厦做网站的seo网站优化策划书
  • html购物网站源码游戏优化大师下载安装
  • 成都设计研究院深圳排名seo公司
  • 安居客网官网入口seo公司网站
  • 好看网站推荐货源搜索引擎优化的分类
  • 给个网站你们知道的国内做网站的公司
  • 做网站老板不发工资我拿尾款青岛网站设计微动力
  • 网站蜘蛛爬行今日最新体育新闻
  • 淘宝网站建设的主要工作googleseo服务公司
  • 有什么网站可以做浏览单站点查询