加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 如何在不解压缩内容的情况下查看.tar.gz存档中特定文

发布时间:2020-12-20 12:26:03 所属栏目:Python 来源:网络整理
导读:对于前abc.tar.gz有 ABC / FILE1.TXT ABC / FILE2.TXT ABC / ABC1 / file3.txt ABC / ABC2 / file4.txt 我需要读取/显示file3.txt的内容而不解压缩文件. 感谢您的任何意见. 解决方法 import tarfilespam = tarfile.open( "abc.tar.gz" )if "abc/abc1/file3.t
对于前abc.tar.gz有

ABC / FILE1.TXT
ABC / FILE2.TXT
ABC / ABC1 / file3.txt
ABC / ABC2 / file4.txt

我需要读取/显示file3.txt的内容而不解压缩文件.

感谢您的任何意见.

解决方法

import tarfile
spam = tarfile.open( "abc.tar.gz" )
if "abc/abc1/file3.txt" in spam.getnames():
    with spam.extractfile( "abc/abc1/file3.txt" ) as ham:
        print ham.read()

见tarfile.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读