【发布时间】:2019-08-19 08:25:58
【问题描述】:
我试图让一个简单的嵌套管道通过 Python 脚本使用子进程工作,但我得到的输出没有意义。
我尝试将diff 的输出重定向到grep 并从grep 重定向到wc,然后检查输出但没有运气。
import subprocess
diff = subprocess.Popen(("diff", "-y", "--suppress-common-lines", "file1.py", "file2.py"), stdout=subprocess.PIPE)
diff.wait()
grep = subprocess.Popen(("grep", "'^'"), stdin=diff.stdout, stdout=subprocess.PIPE)
grep.wait()
output = subprocess.check_output(('wc', '-l'), stdin=grep.stdout)
print(output)
我希望这会导致 file1.py 和 file2.py 之间的行数不同,但我却得到了
b' 0\n'
在命令行中,当我运行diff -y --suppress-common-lines file1.py file2.py | grep '^' | wc -l 时,它会返回一个整数。
【问题讨论】:
-
您是否尝试从
grep.stdout和diff.stdout流中读取以打印其内容?它可能会告诉你更多关于失败的地方。 (看起来wc -l命令运行正常,但没有收到任何内容,因此为 0。)
标签: python linux subprocess pipe