如何在python中使用命令行显示特定uid或用户名的进程?
这就是我到目前为止所知道的,numuid应该是ps-u之后的,是uid。但是,无论numuid是什么,我该怎么做才能将其读入?您只需要将名称作为变量传递,如果您只想查看或存储输出,那么检查输出是最简单的方法:如何在python中使用命令行显示特定uid或用户名的进程?,python,process,command-line-arguments,Python,Process,Command Line Arguments,这就是我到目前为止所知道的,numuid应该是ps-u之后的,是uid。但是,无论numuid是什么,我该怎么做才能将其读入?您只需要将名称作为变量传递,如果您只想查看或存储输出,那么检查输出是最简单的方法: for opt, arg in opts: if opt in ('-u'): arg = numuid ps = subprocess.Popen('ps -u ', shell=True, stdout=subprocess.PIPE) print
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()
对于python 2.6:
from subprocess import check_output
out = check_output(["ps","-u", numuid])
print(out)
当您传递参数列表时,不需要
shell=True
。您可以在python 2.6上执行类似的操作吗?我认为check_输出是在2中引入的。7@shtuff. 是的,只需使用popen并再次传递变量即可
from subprocess import PIPE,Popen
p = Popen(["ps","-u", numuid], stdout=PIPE)
out, err= p.communicate()
print(out)