python subprocess如何将stderr和stdout传递给第二个进程stdin的stdin

时间:2017-04-27 14:50:09

标签: python subprocess

我正在使用子进程来运行涉及将stderr和stdout传递给另一个程序的命令,其形式为command1 2>&1 | command 2。 以下是一个玩具示例。

玩具command 1

sleep 2
echo "this is not an error!"
sleep 2
(>&2 echo "THIS IS AN ERROR")

使用shell=True

run_string = 'bash dummy.sh 2>&1 | tee dummy.log'
ps = subprocess.Popen(run_string, shell=True)

我想删除shell=True,因此无法使用|

我在哪里:

ps = subprocess.Popen(['bash', 'dummy.sh'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = subprocess.check_output(('tee', 'dummy.log'), stdin=ps.stderr) 
#I want stdin to be both stdout and stderr.

我想使用子进程shell=False将stdout + stderr传递给第二个程序。 如何实现这一目标?

0 个答案:

没有答案