【发布时间】:2015-03-03 12:22:44
【问题描述】:
p = Popen(our_cmd, shell=True, stdout=PIPE, stderr=PIPE)
output = p.communicate()[0]
split = output.strip('\n\t\r').split("\n")
我想执行字符串our_cmd中的这个命令
我试过的是这个
my @output = `$our_cmd`; ## here command is executing
my @split = grep(s/\s*$//g, @output); ## the format and putting in new array
@split = split("\n", @split);
我的命令正在执行,但没有正确输入。我需要像 Python 代码一样以数组格式输出。
【问题讨论】:
-
你能举一些你正在解析的输出的例子吗?
-
@AlessandroDaRugna:在那个 Python 代码中,
stderr的返回码和内容被忽略了 -
您的 Python 代码在内存中累积
stderr,然后在最后将其丢弃。要从 shell 命令中获取标准输出行作为列表并丢弃其标准错误:lines = check_output(out_cmd, shell=True, stderr=DEVNULL).splitlines()