Python子进程模块PIPE线

时间:2015-02-02 20:00:08

标签: python pipe subprocess

你对Python子流程模块下的PIPE Lines有什么了解。?

以下是一个例子:

代码1:

from subprocess import *
output = Popen("ls")

代码2

from subprocess import *
output = Popen("ls", stdin=PIPE, stdout=PIPE)

2个代码有什么不同?

提前致谢

1 个答案:

答案 0 :(得分:0)

在第一个代码中,output将是包含当前目录中ls命令结果的对象。

在你的第二段代码中,你正在使用stdout ls命令,stdin只提到PIPE这是错误的,因为你没有提及你正在采取的任何对象stdin

您的第二个代码在这种形式下会更好:

from subprocess import *
output = Popen(["ls"], stdout=PIPE).communicate()[0]

在这里,我使用了communicate的{​​{1}}方法,将subprocess(和stdout)通过管道发送到stderr。它返回stdin的元组,但我们只对(stdout, stderr)(索引0)感兴趣,因此我使用了stdout

相关问题