在Mac OS X上捕获Python子进程输出时出现问题 [英] Problems capturing Python subprocess output on Mac OS X
问题描述
我正在Mac OS 10.6.8上运行Python 3.3.我正在编写一个运行多个子进程的脚本,我想捕获每个子进程的输出并将其记录在一个文件中.我对此感到麻烦.
I'm running Python 3.3 on Mac OS 10.6.8. I am writing a script that runs several subprocesses, and I want to capture the output of each one and record it in a file. I'm having trouble with this.
我首先尝试了以下方法:
I first tried the following:
import subprocess
logFile = open("log.txt", 'w')
proc = subprocess.Popen(args, stdout=logFile, stderr=logFile)
proc.wait()
这产生了一个空的log.txt.在互联网上闲逛了一会之后,我尝试了一下
This produced an empty log.txt. After poking around on the internet for a bit, I tried this instead
import subprocess
proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err = proc.communicate()
logFile = open("log.txt", 'w')
logFile.write(output)
这也产生了一个空的log.txt.因此,我没有写文件,而是尝试将输出打印到命令行:
This, too, produced an empty log.txt. So instead of writing to the file, I tried to just print the output to the command line:
output, err = proc.communicate()
print(output)
print(err)
那产生了这个:
b''
b''
我要运行的进程是fastq_quality_trimmer.它获取一个输入文件,对其进行过滤,然后将结果保存到一个新文件中.它只向stdout写了几行,就像这样
The process I'm trying to run is fastq_quality_trimmer. It takes an input file, filters it, and saves the result to a new file. It only writes a few lines to stdout, like so
Minimum Quality Threshold: 20
Minimum Length: 20
Input: 750000 reads.
Output: 750000 reads.
discarded 0 (0%) too-short reads.
如果我从命令行运行它,然后像这样重定向输出
If I run it from the command line and redirect the output like this
fastq_quality_trimmer -Q 33 -v -t 50 -l 20 -i in.fq -o in_trimmed.fq > log.txt
输出已成功写入log.txt.
the output is successfully written to log.txt.
我想也许当我用Popen调用fastq_quality_trimmer时,它以某种方式无法运行,但是我的脚本所生成的过滤文件与我从命令行运行fastq_quality_trimmer时所生成的文件相同.所以这是有效;我只是无法捕获输出.为了使事情更加混乱,我可以使用与我发布的代码基本相同的代码成功捕获其他进程(echo,其他Python脚本)的输出.
I thought perhaps that fastq_quality_trimmer was somehow failing to run when I called it with Popen, but my script produces a filtered file that is identical to the one produced when I run fastq_quality_trimmer from the command line. So it's working; I just can't capture the output. To make matters more confusing, I can successfully capture the output of other processes (echo, other Python scripts) using code that is essentially identical to what I've posted.
有什么想法吗?我错过了令人眼花obvious乱的明显东西吗?
Any thoughts? Am I missing something blindingly obvious?
推荐答案
您忘记了逗号:
["fastq_quality_trimmer", "-Q", "33" "-v", "-t", "50", "-l", "20", "-i", leftInitial, "-o", leftTrimmed]
将其添加到"33"
和"-v"
之间.
您实质上是在传递参数-Q 33-v
而不是-Q 33 -v
.
You are essentially passing in the arguments -Q 33-v
instead of -Q 33 -v
.
如果两个相邻的字符串之间只有空格,则它们将串联两个字符串:
Python will concatenate two adjacent strings if there is only whitespace between them:
>>> "33", "-v"
('33', '-v')
>>> "33" "-v"
'33-v'
由于-v
是使fastq_quality_trimmer
完全产生输出所必需的冗长的开关,因此它将丢失而保持沉默.
Since -v
is the verbose switch that is required to make fastq_quality_trimmer
produce output at all, it'll remain silent with it missing.
每当调用子流程遇到问题时, triple 都会检查创建的命令行.在args
之前加上['echo']
可以帮助实现这一点:
Whenever you encounter problems with calling a subprocess, triple check the command line created. Pre-pending args
with ['echo']
can help in that:
proc = subprocess.Popen(['echo'] + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, err = proc.communicate()
print(output)
这篇关于在Mac OS X上捕获Python子进程输出时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!