2016-12-22 52 views
-2

如何在python中执行以下子进程命令?使用管道调用子进程

$ ps aux|grep python 

>>> subprocess.check_output(['ps', 'aux', 'grep', 'python']) ? 
+0

您需要创建两个子过程,一个是'PS aux',其他为'grep的python'。将第一个输出管道连接到第二个输入管道。 – Barmar

+2

请参阅http://stackoverflow.com/questions/6780035/python-how-to-run-ps-cax-grep-something-in-python –

回答

-1

你可以做到以下几点:

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

print output