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

求大侠指点一下我的代码,我要做的是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.iplaypy.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)

玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/code/base/b2205.html

相关文章 Recommend

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值