发布于 2019-05-15 09:10:25 | 1523 次阅读 | 评论: 4 | 来源: 网友投递
Python编程语言
Python 是一种面向对象、解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年。Python语法简洁而清晰,具有丰富和强大的类库。它常被昵称为胶水语言,它能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起。
这篇文章主要介绍了Python获取系统所有进程PID及进程名称的方法,涉及Python使用psutil对系统进程进行操作的相关实现技巧,需要的朋友可以参考下
本文实例讲述了Python获取系统所有进程PID及进程名称的方法。分享给大家供大家参考,具体如下:
psutil模块中提供了进程管理方法,引用其中的几个方法就能够获得进程的相关信息。简单写个小脚本测试一下,顺便看看本机Windows7系统中到底运行着多少个进程,进程都是什么。
代码:
# -*- coding: utf-8 -*-
#! python2
#!/usr/bin/python
import psutil
pids = psutil.pids()
for pid in pids:
p = psutil.Process(pid)
print("pid-%d,pname-%s" %(pid,p.name()))
运行结果:
pid-0,pname-System Idle Process
pid-4,pname-System
pid-288,pname-smss.exe
pid-428,pname-csrss.exe
pid-480,pname-wininit.exe
pid-500,pname-csrss.exe
pid-544,pname-services.exe
pid-576,pname-winlogon.exe
pid-588,pname-lsass.exe
pid-596,pname-lsm.exe
pid-716,pname-svchost.exe
pid-800,pname-svchost.exe
pid-864,pname-svchost.exe
pid-932,pname-svchost.exe
pid-1012,pname-svchost.exe
pid-1128,pname-svchost.exe
pid-1324,pname-ZhuDongFangYu.exe
pid-1348,pname-svchost.exe
pid-1468,pname-spoolsv.exe
pid-1608,pname-svchost.exe
pid-1692,pname-360bpsvc.exe
pid-1736,pname-AlibabaProtect.exe
pid-1892,pname-QQProtect.exe
pid-1928,pname-secbizsrv.exe
pid-1996,pname-TBSecSvc.exe
pid-2004,pname-taskhost.exe
pid-356,pname-httpd.exe
pid-348,pname-mysqld.exe
pid-592,pname-dwm.exe
pid-1560,pname-explorer.exe
pid-1304,pname-wwbizsrv.exe
pid-2248,pname-360tray.exe
pid-2320,pname-tomcat6w.exe
pid-2408,pname-360sd.exe
pid-2596,pname-TaobaoProtect.exe
pid-2696,pname-aliwssv.exe
pid-2736,pname-conhost.exe
pid-2996,pname-360bdoctor.exe
pid-3132,pname-360rp.exe
pid-3740,pname-httpd.exe
pid-3072,pname-QQ.exe
pid-3180,pname-svchost.exe
pid-3544,pname-baidupinyin.exe
pid-3788,pname-TXPlatform.exe
pid-4124,pname-svchost.exe
pid-5104,pname-SoftMgrLite.exe
pid-6000,pname-360se.exe
pid-6112,pname-360se.exe
pid-4136,pname-wdswfsafe.exe
pid-2936,pname-360se.exe
pid-5180,pname-360se.exe
pid-4452,pname-360se.exe
pid-5500,pname-360se.exe
pid-1812,pname-360se.exe
pid-2640,pname-eclipsePHP.exe
pid-4556,pname-javaw.exe
pid-2032,pname-360se.exe
pid-6236,pname-QQ.exe
pid-5416,pname-360se.exe
pid-4196,pname-360se.exe
pid-5380,pname-chrome.exe
pid-5220,pname-chrome.exe
pid-4652,pname-chrome.exe
pid-7880,pname-chrome.exe
pid-9168,pname-chrome.exe
pid-6432,pname-chrome.exe
pid-6680,pname-360se.exe
pid-2404,pname-360se.exe
pid-9432,pname-chrome.exe
pid-8692,pname-flashfxp.exe
pid-10116,pname-flashfxp.exe
pid-6628,pname-360se.exe
pid-8504,pname-360se.exe
pid-6464,pname-BFDesktopTips.exe
pid-11764,pname-audiodg.exe
pid-9364,pname-TTPlayer.exe
pid-6356,pname-360se.exe
pid-8808,pname-360se.exe
pid-8640,pname-eclipse.exe
pid-11344,pname-python2.exe
pid-5880,pname-conhost.exe
pid-4656,pname-chrome.exe
pid-3188,pname-360se.exe
pid-9992,pname-360se.exe
pid-12116,pname-cmd.exe
pid-9248,pname-conhost.exe
pid-7208,pname-python2.exe
pid-1832,pname-conhost.exe
注:这里使用了psutil模块,若提示Unresolved import: psutil,则需要使用如下命令进行安装:
pip install psutil
JustTes2020