Python进程psutil库

闲着无聊写个关于进程的东西 参考一下

import psutil, os

def processlist():  #遍历进程列表
    plist = []
    for p in psutil.process_iter():
        try:
            pinfo = p.as_dict(attrs = ['pid', 'name'])
        except psutil.NoSuchProcess:
            pass
        else:
            plist.append(pinfo)
    return plist

def processcheck(): #检查某个进程是否存在
    plist = processlist()
    for i in plist:
        #print(i['name'])   #调试时使用 遍历进程名字
        if process_name == i['name']:
            return i['pid']
    else:
        return False

def processkill():  #结束某个进程
    pid = processcheck()
    if pid == False:
        print("该进程不存在")
    else:
        os.popen("taskkill.exe /F /pid:" + str(pid))

if __name__ == '__main__':
    process_name = "QtScrcpy.exe"
    print(processlist())

 

上一篇:Python运维自动化psutil 模块详解(超级详细)


下一篇:用python来查看电脑的CPU的核心个数