简体   繁体   English

GZip:Python-如何使用gzip.open获取特定行的文件名

[英]GZip: Python - How to get the file name of a particular line using gzip.open

I have a '.tgz' file, I'm reading the content of the '.tgz' file using gzip.open. 我有一个'.tgz'文件,我正在使用gzip.open读取'.tgz'文件的内容。 What is the fastest way to get the file name of a particular line? 获取特定行文件名的最快方法是什么?

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

This functions but quite how useful it is, is anyone's guess. 任何人都猜想,它可以起作用,但是它很有用。
Check https://en.wikipedia.org/wiki/Tar_%28computing%29#Header for the header format. 检查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()

Note: this is for python 2.7.6 on Linux, assuming the archive was created with tar czvf 注意:这适用于Linux上的python 2.7.6,假设存档是使用tar czvf创建的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM