在python脚本中读取tar文件内容而不解压它 [英] reading tar file contents without untarring it, in python script
本文介绍了在python脚本中读取tar文件内容而不解压它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 tar 文件,里面有很多文件.我需要编写一个 python 脚本,该脚本将读取文件的内容并给出总字符数,包括字母总数、空格、换行符等所有内容,而无需解压 tar 文件.
解决方案
你可以使用getmembers()
之后,您可以使用 extractfile()
将成员提取为文件对象.举个例子
import tarfile,os导入系统os.chdir("/tmp/foo")tar = tarfile.open("test.tar")对于 tar.getmembers() 中的成员:f=tar.extractfile(成员)内容=f.read()打印 "%s 有 %d 个换行符" %(member, content.count("\n"))打印 "%s 有 %d 个空格" % (member,content.count(" "))打印 "%s 有 %d 个字符" % (member, len(content))系统退出()tar.close()
对于上面例子中的文件对象f
,你可以使用read()
、readlines()
等
I have a tar file which has number of files within it. I need to write a python script which will read the contents of the files and gives the count o total characters, including total number of letters, spaces, newline characters, everything, without untarring the tar file.
解决方案
you can use getmembers()
>>> import tarfile
>>> tar = tarfile.open("test.tar")
>>> tar.getmembers()
After that, you can use extractfile()
to extract the members as file object. Just an example
import tarfile,os
import sys
os.chdir("/tmp/foo")
tar = tarfile.open("test.tar")
for member in tar.getmembers():
f=tar.extractfile(member)
content=f.read()
print "%s has %d newlines" %(member, content.count("\n"))
print "%s has %d spaces" % (member,content.count(" "))
print "%s has %d characters" % (member, len(content))
sys.exit()
tar.close()
With the file object f
in the above example, you can use read()
, readlines()
etc.
这篇关于在python脚本中读取tar文件内容而不解压它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文