擅长:python、mysql、java
<p>发生的事情是stderr没有被正确重定向。在执行readline时,需要通过python提示符中的以下命令将stderr重定向到stdout。你需要把它移到你的功能上。你知道吗</p>
<pre><code> def run_test(expr=""):
try:
process = sp.Popen('bc',
stdin=sp.PIPE,
stdout=sp.PIPE,
stderr=sp.STDOUT)
process.stdin.write(expr)
result = process.stdout.readline()
process.stdin.close()
retval = process.wait()
return result
except Exception, e:
print e
# correct expression, returns '4'
print run_test('2+2\n')
print run_test('2 / 0\n') # now this stops too
</code></pre>
<p>医生说:<a href="http://docs.python.org/2/library/subprocess.html" rel="nofollow">http://docs.python.org/2/library/subprocess.html</a>
一个特殊值,可用作Popen的stderr参数,并指示标准错误应与标准输出进入同一句柄。你知道吗</p>