我通常使用非常简单的subprocess.check_output
:
process = subprocess.check_output("ps aux", shell=True)
print process #display the list of process
如果我担心stderr
中有什么东西,我会这样使用它:
process = subprocess.check_output("ps aux 2> /dev/null", shell=True)
print process #display the list of process
但是我对nginx -V
有个问题:
modules = subprocess.check_output("nginx -V", shell=True) #display the result
print modules #empty
modules = subprocess.check_output("nginx -V 2> /dev/null", shell=True) #display nothing
print modules #empty
为什么命令nginx -V
的行为不同(都在stderr
中打印)?我如何设计一个解决方案``子流程检查输出`? 你知道吗
将标准错误重定向到shell中的标准输出的方法是
2>&1
,但是最好不要在这里使用shell。你知道吗如果您有较新的Python,也可以考虑切换到^{}
相关问题 更多 >
编程相关推荐