如何在 python 中使用命令行显示特定 uid 或用户名的进程


for opt, arg in opts:
   if opt in ('-u'):
     arg = numuid
     ps = subprocess.Popen('ps -u ', shell=True, stdout=subprocess.PIPE)
     print ps.stdout.read()

这就是我到目前为止所拥有的,Numuid 应该追随 PS -U 并且是 UID。 但是我该怎么做才能让它在 NUMUID 中阅读?

你只需要将名称作为变量传递,如果你只想查看或存储输出check_output最简单的方法:

from subprocess import check_output
out = check_output(["ps","-u", numuid])
print(out)

对于 python 2.6:

from subprocess import PIPE,Popen
p = Popen(["ps","-u", numuid], stdout=PIPE)
out, err= p.communicate()
print(out)

传递参数列表时不需要shell=True

最新更新