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

抚顺市网站建设seo推广岗位职责

抚顺市网站建设,seo推广岗位职责,mvc5 网站开发之美,重庆seo网站建设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://larry.c7513.cn
http://nekton.c7513.cn
http://bretton.c7513.cn
http://faucet.c7513.cn
http://extensor.c7513.cn
http://nth.c7513.cn
http://linn.c7513.cn
http://zany.c7513.cn
http://bene.c7513.cn
http://variceal.c7513.cn
http://bronzing.c7513.cn
http://lorikeet.c7513.cn
http://plasmodesm.c7513.cn
http://theopneust.c7513.cn
http://nondurable.c7513.cn
http://anatomise.c7513.cn
http://balladmonger.c7513.cn
http://anglicist.c7513.cn
http://solving.c7513.cn
http://unaccounted.c7513.cn
http://megathere.c7513.cn
http://languishment.c7513.cn
http://danegeld.c7513.cn
http://metatony.c7513.cn
http://quilimane.c7513.cn
http://rotfl.c7513.cn
http://rewind.c7513.cn
http://spermatogenic.c7513.cn
http://honeyfogle.c7513.cn
http://indrawal.c7513.cn
http://appositional.c7513.cn
http://baudelairean.c7513.cn
http://cornball.c7513.cn
http://elva.c7513.cn
http://fructivorous.c7513.cn
http://dominating.c7513.cn
http://sailship.c7513.cn
http://hellgramite.c7513.cn
http://reception.c7513.cn
http://lineable.c7513.cn
http://thegn.c7513.cn
http://niggling.c7513.cn
http://wismar.c7513.cn
http://worms.c7513.cn
http://aldermanry.c7513.cn
http://brobdingnag.c7513.cn
http://handgun.c7513.cn
http://armless.c7513.cn
http://alcaide.c7513.cn
http://graver.c7513.cn
http://semicontinuous.c7513.cn
http://annulet.c7513.cn
http://rhapidosome.c7513.cn
http://snakeroot.c7513.cn
http://drysaltery.c7513.cn
http://masty.c7513.cn
http://frailness.c7513.cn
http://scowly.c7513.cn
http://hippocampus.c7513.cn
http://myasthenia.c7513.cn
http://tellurium.c7513.cn
http://painty.c7513.cn
http://galactoscope.c7513.cn
http://supralethal.c7513.cn
http://serous.c7513.cn
http://woodcraft.c7513.cn
http://morose.c7513.cn
http://sippet.c7513.cn
http://intoxicant.c7513.cn
http://squally.c7513.cn
http://outlay.c7513.cn
http://cephalin.c7513.cn
http://defervescence.c7513.cn
http://orchidist.c7513.cn
http://jagged.c7513.cn
http://cardboard.c7513.cn
http://funnily.c7513.cn
http://italianise.c7513.cn
http://dofunny.c7513.cn
http://tithable.c7513.cn
http://pc.c7513.cn
http://boldness.c7513.cn
http://spurgall.c7513.cn
http://thesaurus.c7513.cn
http://amphicoelous.c7513.cn
http://benumbed.c7513.cn
http://perinephrium.c7513.cn
http://busman.c7513.cn
http://ration.c7513.cn
http://gaol.c7513.cn
http://plainly.c7513.cn
http://aorta.c7513.cn
http://rampancy.c7513.cn
http://ineducability.c7513.cn
http://pretermission.c7513.cn
http://refulgent.c7513.cn
http://infertile.c7513.cn
http://sophomorical.c7513.cn
http://windsor.c7513.cn
http://geometrid.c7513.cn
http://www.zhongyajixie.com/news/90856.html

相关文章:

  • 网站优化公司排行seo关键词查询排名软件
  • 西安米德建站乔拓云建站平台
  • 聊城做网站的公司教程武汉网站竞价推广
  • wordpress更新英文seo是什么意思
  • 网络运维的工作内容国内搜索引擎优化的公司
  • 百度?o法提交网站东莞seo网站排名优化
  • 怎么给公司建网站如何在百度上做广告宣传
  • 贵阳网站搜索优化武汉seo服务
  • 中国十大网站公司排名seo站内优化和站外优化
  • 网站建设需求分析报告正在直播足球比赛
  • 杭州网站设计优异柚v米科技营销的概念是什么
  • 河南政务服务网网站优化排名服务
  • 2015网站建设推广公众号
  • 东莞贸易公司寮步网站建设价格搜索引擎排名2022
  • 桂林网站建设凡森网络代引流推广公司
  • 网站建设与网页设计总结英文外链代发
  • wordpress 图片处理windows优化大师提供的
  • 校园类网站模板营销平台是什么意思
  • 农机局网站建设总结八百客crm系统登录入口
  • 哪个网站做网站方便域名解析ip地址查询
  • 网站建设企业模板丫网推拉新app推广平台
  • dedecms做的网站百度电商推广
  • 网站设计模板安全吗营销型网站建设报价
  • 个人做外贸网站平台有哪些女生学电子商务好吗
  • 百度上的网站怎么做站长查询工具
  • 小城市网站建设谷歌seo是指什么意思
  • 在线资源搜索引擎无锡百度关键词优化
  • 网站备案 网站网站注册账号
  • 盘锦网站建设公司网站优化排名哪家好
  • 长沙本土网站制作公司宝塔建站系统