子流程不提供Flow输出

时间:2015-08-14 16:34:44

标签: python subprocess

我试图从python脚本调用facebook/flow命令。它没有给出预期的resultresult变量为(b'', None)

p = subprocess.Popen(["/usr/local/bin/flow", "--help"], stdout=subprocess.PIPE)
result = p.communicate()    
print(result)

我在Mac OS X Yosemite上安装了流程,并且在已使用的目录中存在可执行文件。我也安装了R,当我使用下面的代码时,它会生成有效的输出( b'b'\nUsage: R [options] [< infile] [> outfile]\n', None)

p = subprocess.Popen(["/usr/local/bin/R", "--help"], stdout=subprocess.PIPE)
result = p.communicate()    
print(result)

subprocess为什么p = subprocess.Popen(["/usr/local/bin/flow", "--help"], stderr=subprocess.PIPE) result = p.communicate() print(result) 仅因flow而失败?我错过了什么吗?

修改

使用下面的代码生成预期的输出

var grid = new Grid ();
grid.RowDefinitions.Add (new RowDefinition { Height = GridLength.Auto });
grid.RowDefinitions.Add (new RowDefinition { Height = new GridLength (1, GridUnitType.Star) });

var stacklayout1 = new StackLayout { HeightRequest = 100, BackgroundColor = Color.Red };
var stacklayout2 = new StackLayout { BackgroundColor = Color.Blue };

Grid.SetRow (stacklayout2, 1);

grid.Children.Add (stacklayout1);
grid.Children.Add (stacklayout2);

MainPage = new ContentPage { Content = grid };  

对此行为的任何澄清?

1 个答案:

答案 0 :(得分:1)

最有可能的是,当您执行命令时 -

/usr/local/bin/flow --help

该过程将其输出写入stderr而不是stdout。因此,只有在PIPE stderr输出时才能获得输出。示例 -

p = subprocess.Popen(["/usr/local/bin/flow", "--help"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

这将在stdout和stderr中进行管道传输,然后您可以使用所需的输出。

Popen.communicate()返回格式为(stdout, stderr)的元组 - 因此元组中的第二个元素将包含您需要的数据。