Python Subprocess.Popen即使在stdout=Subprocess.PIPE的情况下也会在屏幕上显示输出)
我正在使用多个命令来运行: e、 g.Python Subprocess.Popen即使在stdout=Subprocess.PIPE的情况下也会在屏幕上显示输出),python,Python,我正在使用多个命令来运行: e、 g.cd foo/bar;.//运行\u this-arg1-arg2=“是的\u更多arg1 arg2”arg3=/my/path finalarg 运行时使用: p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) (out, err) = p.communicate() 但这会在屏幕上显示输出(Python 2.7.5) out是空字符串。您有shell=True,因此您基本上是在读
cd foo/bar;.//运行\u this-arg1-arg2=“是的\u更多arg1 arg2”arg3=/my/path finalarg
运行时使用:
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
(out, err) = p.communicate()
但这会在屏幕上显示输出(Python 2.7.5)
out是空字符串。您有
shell=True
,因此您基本上是在读取生成的shell的标准输出,而不是要运行的程序的标准输出
我猜您正在使用shell=True
来适应目录更改。幸运的是,subprocess
可以为您解决这个问题(通过cwd
关键字参数传递目录):
根据我的评论,我也添加了stderr,这很有效
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
可能是打印到stderr上了吗?您可能还需要将其重定向到
子流程.PIPE
。可能您有stderr,您应该添加stderr=subprocess.STDOUT
,以便将其连接到outputaha。。事实就是这样!!谢谢你们两位-如果在Popen()?
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT)