使用Python subprocess将stdout重定向到stdin?

1 投票
3 回答
6065 浏览
提问于 2025-04-17 08:10

我正在通过shell调用一个程序,这个程序会把一个二进制文件输出到标准输出(STDOUT)。

我使用Popen()来调用这个程序,然后我想把这个输出流传递给一个Python包里的函数(叫做“pysam”),但这个函数不能直接读取Python的文件对象,但是可以从标准输入(STDIN)读取。所以我想把shell命令的输出从标准输出转到标准输入。

我该如何在Popen/subprocess模块中做到这一点呢?这是我调用shell程序的方式:

p = subprocess.Popen(my_cmd, stdout=subprocess.PIPE, shell=True).stdout

这段代码会读取“my_cmd”的标准输出,并在变量p中获取到这个输出流。由于我的Python模块不能直接从“p”读取,所以我试图通过以下方式把“my_cmd”的标准输出重定向回标准输入:

p = subprocess.Popen(my_cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, shell=True).stdout

然后我调用我的模块,它使用“-”作为标准输入的占位符:

s = pysam.Samfile("-", "rb")

上面的调用意味着从标准输入(用“-”表示)读取,并将其作为二进制文件读取(“rb”)。

当我尝试这样做时,我只得到二进制输出显示在屏幕上,看起来Samfile()函数无法读取它。即使我去掉对Samfile的调用,我觉得问题出在我对Popen的调用,而不是后续的步骤。

编辑:针对回答,我尝试了:

sys.stdin = subprocess.Popen(tagBam_cmd, stdout=subprocess.PIPE, shell=True).stdout
print "Opening SAM.."                                                                                            
s = pysam.Samfile("-","rb")
print "Done?"
sys.stdin = sys.__stdin__    

这似乎卡住了。我得到了输出:

Opening SAM..

但它从来没有超过Samfile("-", "rb")这一行。你知道为什么吗?

有没有办法解决这个问题?

编辑2:我添加了一个Pysam文档的链接,希望能有所帮助,我真的搞不明白。文档页面是:

http://wwwfgu.anat.ox.ac.uk/~andreas/documentation/samtools/usage.html

关于流的具体说明在这里:

http://wwwfgu.anat.ox.ac.uk/~andreas/documentation/samtools/usage.html#using-streams

特别是:

""" Pysam不支持从真正的Python文件对象读取和写入,但它支持从标准输入和标准输出读取和写入。以下示例从标准输入读取并写入到标准输出:

infile = pysam.Samfile( "-", "r" )
outfile = pysam.Samfile( "-", "w", template = infile )
for s in infile: outfile.write(s)

它也可以处理BAM文件。以下脚本将标准输入中的BAM格式文件转换为标准输出中的SAM格式文件:

infile = pysam.Samfile( "-", "rb" )
outfile = pysam.Samfile( "-", "w", template = infile )
for s in infile: outfile.write(s)

注意,只有文件打开模式需要从r改为rb。 """

所以我只是想把从Popen读取的输出流(即标准输出)重定向到标准输入,这样我就可以像上面提到的那样使用Samfile("-", "rb")。

谢谢。

3 个回答

0

如果你的系统支持的话,你可以使用 这种 /dev/fd/# 的文件名

process = subprocess.Popen(args, stdout=subprocess.PIPE)
samfile = pysam.Samfile("/dev/fd/%d" % process.stdout.fileno(), "rb")
2

在处理pysam的具体情况下,我找到了一种解决方法,就是使用命名管道(可以参考这个链接:http://docs.python.org/library/os.html#os.mkfifo)。命名管道就像一个普通的文件,可以被访问。一般来说,你希望管道的接收方(也就是读取数据的部分)在你开始写入数据之前就先开始监听,这样可以确保不会漏掉任何信息。不过,正如你提到的,如果在标准输入(stdin)上没有任何东西注册,pysam.Samfile("-", "rb")就会卡住。

假设你正在处理一个需要花费一定时间的计算(比如在把数据传入pysam之前先对bam文件进行排序),你可以先开始这个计算,然后在输出数据之前先监听这个流:

import os
import tempfile
import subprocess
import shutil
import pysam

# Create a named pipe
tmpdir = tempfile.mkdtemp()
samtools_prefix = os.path.join(tmpdir, "namedpipe")
fifo = samtools_prefix + ".bam"
os.mkfifo(fifo)

# The example below sorts the file 'input.bam',
# creates a pysam.Samfile object of the sorted data,
# and prints out the name of each record in sorted order

# Your prior process that spits out data to stdout/a file
# We pass samtools_prefix as the output prefix, knowing that its
# ending file will be named what we called the named pipe
subprocess.Popen(["samtools", "sort", "input.bam", samtools_prefix])

# Read from the named pipe
samfile = pysam.Samfile(fifo, "rb")

# Print out the names of each record
for read in samfile:
    print read.qname

# Clean up the named pipe and associated temp directory
shutil.rmtree(tmpdir)
2

我有点困惑,为什么在使用 stdout=subprocess.PIPE 的时候,输出会显示二进制数据。不过,整体问题是,如果你想让 pysam 使用 sys.stdin,你需要对它进行一些调整。

举个例子:

sys.stdin = subprocess.Popen(my_cmd, stdout=subprocess.PIPE, shell=True).stdout
s = pysam.Samfile("-", "rb")
sys.stdin = sys.__stdin__ # restore original stdin

更新:这假设 pysam 是在 Python 解释器的上下文中运行的,因此当指定 "-" 时,它意味着 Python 解释器的标准输入。可惜的是,它并不是这样;当指定 "-" 时,它直接从文件描述符 0 读取数据。

换句话说,它并没有使用 Python 的标准输入概念(sys.stdin),所以替换它对 pysam.Samfile() 没有任何影响。而且,无法将 Popen 调用的输出“推送”到文件描述符 0,因为它是只读的,另一端连接的是你的终端。

将输出放到文件描述符 0 的唯一真正方法是把它移动到一个额外的脚本中,并将两个脚本连接起来。这样可以确保第一个脚本中 Popen 的输出会最终出现在第二个脚本的文件描述符 0 上。

所以,在这种情况下,你最好的选择是把这个分成两个脚本。第一个脚本会调用 my_cmd,并将它的输出作为输入传递给第二个脚本的 Popen,这个第二个脚本会调用 pysam.Samfile("-", "rb")。

撰写回答