如何仅提取 .tar.gz 成员的文件? [英] How do I extract only the file of a .tar.gz member?

查看:46
本文介绍了如何仅提取 .tar.gz 成员的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是解压 .tar.gz 文件,而不是解压文件的子目录.

My goal is to unpack a .tar.gz file and not its sub-directories leading up to the file.

我的代码基于这个问题 除了不是解压 .zip 我正在解压 .tar.gz 文件.

My code is based off this question except instead of unpacking a .zip I am unpacking a .tar.gz file.

我问这个问题是因为我得到的错误非常模糊并且没有在我的代码中识别问题:

I am asking this question because the error I'm getting is very vague and doesn't identify the problem in my code:

import os
import shutil
import tarfile

with tarfile.open('RTLog_20150425T152948.gz', 'r:gz') as tar:
    for member in tar.getmembers():
        filename = os.path.basename(member.name)
        if not filename:
            continue

        # copy file (taken from zipfile's extract)
        source = member
        target = open(os.path.join(os.getcwd(), filename), "wb")
        with source, target:
            shutil.copyfileobj(source, target)

如您所见,我从链接问题中复制了代码,并尝试对其进行更改以处理 .tar.gz 成员而不是 .zip 成员.运行代码后,我收到以下错误:

As you can see I copied the code from the linked question and tried to change it to deal with .tar.gz members instead of .zip members. Upon running the code I get the following error:

Traceback (most recent call last):
  File "C:\Users\dzhao\Desktop\123456\444444\blah.py", line 27, in <module>
    with source, target:
AttributeError: __exit__

根据我所做的阅读,shutil.copyfileobj 将两个类文件"对象作为输入.member 是一个 TarInfo 对象.我不确定 TarInfo 对象是否是类似文件的对象,所以我尝试将这一行从:

From the reading I've done, shutil.copyfileobj takes as input two "file-like" objects. member is a TarInfo object. I'm not sure if a TarInfo object is a file-like object so I tried changing this line from:

source = member #to
source = open(os.path.join(os.getcwd(), member.name), 'rb')

但这可以理解地引发了找不到文件的错误.

But this understandably raised an error where the file wasn't found.

我不明白什么?

推荐答案

这段代码对我有用:

import os
import shutil
import tarfile

with tarfile.open(fname, "r|*") as tar:
    counter = 0

    for member in tar:
        if member.isfile():
            filename = os.path.basename(member.name)
            if filename != "myfile": # do your check
                continue

            with open("output.file", "wb") as output: 
                shutil.copyfileobj(tar.fileobj, output, member.size)

            break # got our file

        counter += 1
        if counter % 1000 == 0:
            tar.members = [] # free ram... yes we have to do this manually

但您的问题可能不是提取,而是您的文件确实不是 .tar.gz 而只是 .gz 文件.

But your problem might not be the extraction, but rather that your file is indeed no .tar.gz but just a .gz file.

由于 python 试图调用 __enter__ 成员对象的函数(不存在).

Also your getting the error on the with line because python is trying to call the __enter__ function of the member object (wich does not exist).

这篇关于如何仅提取 .tar.gz 成员的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆