带有管道的子进程调用

Dav*_*542 0 python subprocess

我将如何在 python 中执行以下子进程命令?

$ ps aux|grep python

>>> subprocess.check_output(['ps', 'aux', 'grep', 'python']) ?
Run Code Online (Sandbox Code Playgroud)

Dav*_*542 5

您可以执行以下操作:

ps = subprocess.Popen(('ps', 'aux'), stdout=subprocess.PIPE)
output = subprocess.check_output(('grep', 'python'), stdin=ps.stdout)
ps.wait()

print output
Run Code Online (Sandbox Code Playgroud)