gpt4 book ai didi

python - 如何获取包含管道的 Python 子进程命令的输出?

转载 作者:行者123 更新时间:2023-12-01 06:40:49 30 4
gpt4 key购买 nike

我有:

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.

我做错了什么?

最佳答案

试试这个:

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"))

关于python - 如何获取包含管道的 Python 子进程命令的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59462803/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com