本文实例讲述了Python获取系统所有进程PID及进程名称的方法。分享给大家供大家参考,具体如下:
psutil模块中提供了进程管理方法,引用其中的几个方法就能够获得进程的相关信息。简单写个小脚本测试一下,顺便看看本机Windows7系统中到底运行着多少个进程,进程都是什么。
代码:
1
2
3
4
5
6
7
8
|
# -*- 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,则需要使用如下命令进行安装:
1
|
pip install psutil |
希望本文所述对大家Python程序设计有所帮助。
原文链接:https://blog.csdn.net/grey_csdn/article/details/70150438