【问题标题】:How can I get the output of a Python subprocess command that contains a pipe?如何获取包含管道的 Python 子进程命令的输出?
【发布时间】:2019-12-24 01:09:44
【问题描述】:

我有:

cmd_array = ['head', '-n', str(source_noise_end), "data/noise/" + source + '_16k.dat', '|', 'tail', '-' + str(source_noise_start)]
source_noise = subprocess.check_output(cmd_array)

该命令在我输入 Linux 时有效。我得到subprocess.CalledProcessError: Command '['head', '-n', '2366468', 'data/noise/white_16k.dat', '|', 'tail', '-2183988']' returned non-zero exit status 1.

我做错了什么?

【问题讨论】:

  • 您是否尝试过指定完整路径而不是data/...

标签: python subprocess pipe


【解决方案1】:

试试这个:

import subprocess

# cmd contains shell command
cmd="your command shell"

process = subprocess.Popen(cmd,shell=True,stdin=None,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
# The output from your shell command
result=process.stdout.readlines()
if len(result) >= 1:
    for line in result:
        print(line.decode("utf-8"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-07
    • 2016-04-18
    • 2021-10-28
    • 2018-11-03
    相关资源
    最近更新 更多