将更多/多个参数传递给Python subprocess.call

时间:2018-01-20 10:48:00

标签: python shell subprocess

我可以运行ps aux | grep python_script.py并获取运行脚本的进程的详细信息。我希望能够从python脚本运行相同的命令。

from subprocess import call
call(["ps", "aux"])

运行良好,但尝试了这些行,但都返回错误,

call(["ps", "aux", "|", "grep", "python_script.py"])
call(["ps", "aux", "| grep python_script.py"])

返回错误,有人可以告诉我如何从命令行运行ps aux | grep python_script.py

1 个答案:

答案 0 :(得分:1)

如果您对安全性不太体贴,可以将shell参数设置为True。然后,它将代替list,取一个string作为命令。通过这种方式,它就像一个壳。

from subprocess import call
call("ps aux | grep python_script.py", shell=True)