如何从 python 管道 tar.extractall [英] How to pipe tar.extractall from python
问题描述
我正在使用 python 的 tarfile 模块提取 tarball.我不希望将提取的文件写入磁盘,而是直接通过管道传输到另一个程序,特别是 bgzip.我也在尝试为此使用 StringIO,但我什至在那个阶段卡住了 - tarball 被提取到磁盘上.
I'm extracting a tarball using the tarfile module of python. I don't want the extracted files to be written on the disk, but rather get piped directly to another program, specifically bgzip. I'm also trying to use StringIO for that matter, but I get stuck even on that stage - the tarball gets extracted on the disk.
#!/usr/bin/env python
import tarfile, StringIO
tar = tarfile.open("6genomes.tgz", "r:gz")
def enafun(members):
for tarkati in tar:
if tarkati.isreg():
yield tarkati
reles = StringIO.StringIO()
reles.write(tar.extractall(members=enafun(tar)))
tar.close()
那么我如何正确地通过管道传输 tar.extractall 的输出?
How then do I pipe correctly the output of tar.extractall?
推荐答案
不能使用 extractall 方法,但可以使用 getmembers 和 extractfile> 方法:
You cannot use extractall method, but you can use getmembers and extractfile methods instead:
#!/usr/bin/env python
import tarfile, StringIO
reles = StringIO.StringIO()
with tarfile.open("6genomes.tgz", "r:gz") as tar:
for m in tar.members():
if m.isreg():
reles.write(tar.extractfile(m).read())
# do what you want with "reles".
根据文档,extractfile() 方法可以采用 TarInfo 并返回一个类文件对象.然后,您可以使用 read() 获取该文件的内容.
According to the documentation, extractfile() method can take a TarInfo and will return a file-like object. You can then get the content of that file with read().
我在评论中添加了您问我的内容,因为评论中的格式似乎无法正确呈现.
I add what you asked me in comment as formatting in comment seems not to render properly.
#!/usr/bin/env python
import tarfile
import subprocess
with tarfile.open("6genomes.tgz", "r:gz") as tar:
for m in tar.members():
if m.isreg():
f = tar.extractfile(m)
new_filename = generate_new_filename(f.name)
with open(new_filename, 'wb') as new_file:
proc = subprocess.Popen(['bgzip', '-c'], stdin=subprocess.PIPE, stdout=new_file)
proc.stdin.write(f.read())
proc.stdin.close()
proc.wait()
f.close()
这篇关于如何从 python 管道 tar.extractall的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!