玩蛇网提供最新Python编程技术信息以及Python资源下载!

python调用win7命令清理系统无用进程方法

python 培训

求大侠指点一下我的代码,我要做的是python调用win7命令清理系统无用进程方法。

我的思路是:
用系统命令 tasklist 列出当前running进程;
检索出无用进程的PID;
用系统命令 taskkill 杀掉进程。

python调用win7命令清理系统无用进程方法代码如下:


# -*- coding: cp936 -*-
import os,string
import re
#coding=utf-8

# re rule,to search process PID
rule = re.compile('\s\d+\s')

#www.iplaypython.com

# Get the Message of the all running process and PID  
ProcMessage = os.popen('tasklist').readlines()

# Process list to be killed
KillProclist = ['PPLiveU.exe',
                'wireshark.exe',
                'BitCometService.exe',
                'BitComet.exe',
                'FTPServer.exe',
                'QvodTerminal.exe',
                'PPLive.exe',
                'PPStream.exe',
                'Uedit32.exe',
                'PPSAP.exe',
                'emule.exe',
                'QvodPlayer.exe',
                'wireshark.exe ',
                'SogouCloud.exe',
                'PPAP.exe',
                'AcroRd32.exe' ,
                'firefox.exe',
                'dwm.exe',
                'IcbcDaemon.exe',]

#Store the process name : PID
table={}

def SearchPID(temp):  # To search process PID by Name
    '''Find Proccess Name,Return PID'''
    print 'Proc Name     status     PID'
    for eachline in ProcMessage: # Get a list of running process message to match
        for sub in temp:
            if eachline.find(sub)==0: # if 0 ,Find the process to be killed
                ret = re.search(rule,eachline) # Get the PID
                if ret is not None:
                    print sub,'  running  ',ret.group(0)
                    table.update({sub:ret.group(0)}) # Add {process name:PID} to the Table list
   # print table
    if table == {}:
        print 'No useless process is running!'
    return table

def KillPID(temp):
    for key in temp.keys():
        # Use system cmd TaskKill /T   终止指定的进程和由它启用的子进程
        #                         /F   强制终止
        #                         /IM  指定终止进程的映像名称
        #                         /PID 指定要终止进程的PID
        cmd='TaskKill /T /F /PID %s' % (temp[key]) 
        #print '进程名称:',key
        os.popen(cmd)       # carry out the cmd
        print  'Kill process [',key,'] Successful!'        
       
if __name__=='__main__':
    
   SearchRet=SearchPID(KillProclist)
   
   KillPID(SearchRet)
                        

玩蛇网原创,转载请注明文章出处和来源网址:http://www.iplaypython.com/code/base/b2205.html



微信公众号搜索"玩蛇网Python之家"加关注,每日最新的Python资讯、图文视频教程可以让你一手全掌握。强烈推荐关注!

微信扫描下图可直接关注

玩蛇网Python新手QQ群,欢迎加入: ① 240764603 玩蛇网Python新手群
文章发布日期:2016-06-15 13:50 玩蛇网 www.iplaypython.com

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
相关文章推荐
别人正在看
特别推荐
去顶部去底部