GZip:Python如何使用gzip.open打开

2024-04-25 17:38:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个'.tgz'文件,我正在使用读取'.tgz'文件的内容gzip.open打开. 获取特定行的文件名的最快方法是什么?你知道吗

with gzip.open('sample.tgz','r') as fin:
    for line in fin:
        error = checkForError(line)
        if error:
            # get the file name in which that particular error line was present
        else:
            pass

Tags: 文件sample方法in内容for文件名as
1条回答
网友
1楼 · 发布于 2024-04-25 17:38:21

这个功能很有用,但它到底有多有用,谁都猜不透。
检查https://en.wikipedia.org/wiki/Tar_%28computing%29#Header以获取标头格式。你知道吗

import gzip
fin = gzip.open("test.txt.tar","r")
for i in fin:
    if "ustar" in i:
        fname,b = i.split("0",1)
        print fname
    else:
        print i
fin.close()

注意:这是针对Linux上的Python2.7.6的,假设存档是用tar czvf创建的

相关问题 更多 >