我有一个任务列表文件,它有firefox , atom , gnome-shell
我的代码
import psutil
with open('tasklist', 'r') as task:
x = task.read()
print (x)
print ([p.info for p in psutil.process_iter(attrs=['pid', 'name']) if x in p.info['name']])
期望输出
[{'pid': 413, 'name': 'firefox'}]
[{'pid': 8416, 'name': 'atom'}]
[{'pid': 2322, 'name': 'gnome-shell'}]
与上面的答案类似,但从问题来看,您似乎只对所有正在运行的任务的子集感兴趣(例如firefox、atom和gnomeshell)
您可以将感兴趣的任务放入一个列表中..然后循环执行所有进程,只将与列表匹配的进程附加到最终输出,如下所示:
这将为您提供所需的列表输出,其中每个列表都有一个带有pid和名称键的字典…您可以将输出调整为您喜欢的任何格式
您请求的确切输出可通过以下方式获得:
对于Unix:
相关问题 更多 >
编程相关推荐