繁体   English   中英

使用Python子进程重定向标准输出到标准输入?

[英]using Python subprocess to redirect stdout to stdin?

我正在使用子过程模块从shell调用程序,该子过程模块将二进制文件输出到STDOUT。

我使用Popen()调用程序,然后将流传递给Python包(称为“ pysam”)中的一个函数,不幸的是它无法使用Python文件对象,但可以从STDIN中读取。 所以我想做的是将shell命令的输出从STDOUT转到STDIN。

如何在Popen /子过程模块中完成此操作? 这是我调用shell程序的方式:

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

这将读取“ my_cmd”的STDOUT输出,并在p中获得流。 由于我的Python模块无法直接从“ p”读取,因此我尝试使用以下命令将“ my_cmd”的STDOUT重定向回STDIN:

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

然后,我调用我的模块,该模块使用“-”作为STDIN的占位符:

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

上面的调用仅意味着从STDIN读取(表示为“-”)并将其读取为二进制文件(“ 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文件对象读取和写入,但是它支持从stdin和stdout读取和写入。以下示例从stdin读取并写入stdout:

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

它还将与BAM文件一起使用。 以下脚本将stdin上的BAM格式的文​​件转换为stdout上的SAM格式的文​​件:

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

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

因此,我只想从Popen读取stdout的流,并将其重定向到stdin,以便可以使用Samfile(“-”,“ rb”),因为上面的部分说明是可能的。

谢谢。

在处理pysam的特定情况下,我能够使用命名管道(http://docs.python.org/library/os.html#os.mkfifo)解决此问题,该管道可以是像常规文件一样访问。 通常,您希望管道的使用者(读者)在开始写入管道之前先进行监听,以确保您不会错过任何内容。 但是,如果在stdin上尚未注册任何内容,则pysam.Samfile(“-”,“ rb”)将如您上面指出的那样挂起。

假设您要处理花费大量时间的先前计算(例如,在将bam传递给pysam之前对其进行排序),则可以启动该先前的计算,然后在流输出之前监听流:

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)

我有点困惑,如果您使用stdout=subprocess.PIPE sys.stdin则在stdout上看到二进制文件,但是,总体问题是,如果您想欺骗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解释器的stdin。 不幸的是,事实并非如此。 当指定“-”时,它将直接从文件描述符0读取。

换句话说,它没有使用Python的stdin(sys.stdin)概念,因此替换它对pysam.Samfile()无效。 也不可能从Popen调用中获取输出,并以某种方式将其“推”到文件描述符0上。 它是只读的,另一端连接到您的终端。

将输出输出到文件描述符0的唯一真实方法是将其移至其他脚本,然后将两个脚本从第一个脚本连接在一起。 这确保了第一个脚本中Popen的输出将最终出现在第二个脚本的文件描述符0上。

因此,在这种情况下,最好的选择是将其分为两个脚本。 第一个将调用my_cmd并获取其输出,并将其用作另一个调用pysam.Samfile(“-”,“ rb”)的Python脚本的第二个Popen的输入。

如果您的系统支持它; 您可以使用/dev/fd/#文件名

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM