我需要在终端中运行一个进程来获取它的输出。
import subprocess
subprocess.check_output(["my_util", "some_file.txt", " | grep 'Some data1' | awk '{print $2}'"])
#or
subprocess.check_output(["my_util", "full_path/some_file.txt", "| grep 'Some data1'", "| awk '{print $2}'"])
在 REPL 中什么也没有发生,而在终端中手动运行它会给我正确的输出。
更新:
崇高文本的输出:
my_util FAIL formats: can't open input pipe `| grep 'Sample data1'': premature EOF
my_util FAIL formats: can't open input pipe `| awk '{print $2}'': premature EOF
Traceback (most recent call last):
File "/test1.py", line 4, in <module>
"| grep 'Sample data1'", "| awk '{print $2}'"])
File "/usr/lib/python2.7/subprocess.py", line 544, in check_output
raise CalledProcessError(retcode, cmd, output=output)
subprocess.CalledProcessError: Command '["my_util", "full_path/some_file.txt", "| grep 'Some data1'", "| awk '{print $2}'"]' returned non-zero exit status 2