gpt4 book ai didi

python - 如何打开 tar 文件并获取其文件之一内的数据?

转载 作者:太空宇宙 更新时间:2023-11-03 20:16:47 25 4
gpt4 key购买 nike

这是我尝试过的代码:

import tarfile

# Opening zipped tarfile archive
t = tarfile.open(r'C:\Users\Luke\Desktop\my data.gz', "r:gz")

t.getmembers() #Showing members within tarfile archive

它打印这个:

TarInfo './._SA00000' at 0x2a9431ea430,
TarInfo 'SA00000' at 0x2a9431ea5c0, #theres more members didn't want to show them all

我尝试过:

x = t.extract('SA00000')

print(x)

它打印

我真的不明白。我已在 notepad 中打开 tarfile,所有数据都在那里。

不知道这是否有帮助,但我在 Windows 10 上使用 python 3,并且数据是从 MacOS 提供给我的。

最佳答案

Tar 文件只是 Linux 文件分组,没有真正的压缩。 tar.gz 是相同的东西,但经过压缩。无论您有 tar 还是 tar.gz 文件,您只需在压缩软件(推荐 7zip)中打开文件即可查看其内部结构。

如果您希望以编程方式执行此操作,我仍然建议使用 7zip,但在名为 7za.exe 的独立 CLI 版本中(7-Zip Extra:独立控制台版本、7z DLL、Far Manager 插件)。

这将为您提供 7zip 的所有功能,而不会导致 GUI 臃肿。您将能够从您的应用程序中调用它;看起来您使用的是 python,因此您可能会使用 subprocess 模块。这是一个例子:

def decompress():
out=subprocess.call("7za.exe e -y -o'OUTPUT_FILE_NAME_1.tar' INPUT_FILENAME_1.tar.gz",stdout=fout,stderr=ferr)
output=fout.read())
errors = ferr.read()
if !errors:
out=subprocess.call("7za.exe x -y -o'OUTPUT_FILE_NAME_2' OUTPUT_FILE_NAME_1.tar ",stdout=fout,stderr=ferr)
output=fout.read())
errors = ferr.read()

您最终将得到一个名为 OUTPUT_FILE_NAME_2 的文件夹或文件,然后您可以在其中行走,就好像 tar/tar.gz 文件从未存在一样。第一个调用解压缩“tarball”,第二个调用将其提取。

7za.exe reference

关于python - 如何打开 tar 文件并获取其文件之一内的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58401000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com