问题
I have:
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)
The command is valid when I type it into Linux. I get subprocess.CalledProcessError: Command '['head', '-n', '2366468', 'data/noise/white_16k.dat', '|', 'tail', '-2183988']' returned non-zero exit status 1.
What am I doing wrong?
回答1:
Try with this :
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"))
来源:https://stackoverflow.com/questions/59462803/how-can-i-get-the-output-of-a-python-subprocess-command-that-contains-a-pipe