Python3日常:一键灭掉Chrome浏览器software_reporter_tool.exe进程

Chrome每次自动更新后,出现software_reporter_tool.exe占CPU的问题

在日常使用Chrome经常遇到风扇突然狂转的问题,网上搜了一下才发现Chrome目录下会有这样一个程序software_reporter_tool.exe在狂吃CPU(文件位置一般在C:\Users\name\AppData\Local\Google\Chrome\User Data\SwReporter\42.206.200\software_reporter_tool.exe)

解决方案,删掉,并用同名文件夹占位

网上给出的解决方案是,删掉文件,然后用一个同名文件夹占位就行了。
结果好景不长,隔了一段时间Chrome自动更新了,路径变了,比如"SwReporter\42.212.200",可恶的software_reporter_tool.exe又出现了。

考虑用代码实现上面步骤

每次遇到问题,都去手动结束进程,然后去替换文件夹比较烦(平时有登陆不同账号多开Chrome的需求,所以Chrome不止一个),于是便想每次遇到问题的时候,能不能直接用python一步解决。

所以就写了下面代码:
大概思路就是,申请管理员权限,结束所有正在运行的software_reporter_tool.exe进程,找到进程对应的路径,删除该文件,创建文件夹占位。

执行后效果如下:
在这里插入图片描述
在这里插入图片描述

程序实现代码

from __future__ import print_function
import os
import sys
import subprocess
import locale
import time
import ctypes



'''
Author:https://blog.csdn.net/watfe
Date:2019-08-21
'''


# pyinstaller -F kill_Chrome_software_reporter_tool.exe.py 




banner = '''
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 

                    解决software_reporter_tool.exe高占用问题

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
使用方法:
    当打开Chrome发现有风扇狂响 或 Win任务管理器中有software_reporter_tool.exe在运行时
    双击快捷方式运行,即可完成优化
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
'''
print(banner)





def cmd(command):
    # 执行cmd命令
    result = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE) 
    out,err = result.communicate()
    return out


def is_admin():
    # 获取管理员权限
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False


filename = 'software_reporter_tool.exe'

try:
    
    if is_admin():
        # 可能用到管理员权限执行的部分写在这里

        # 取当前系统代码表,也就是编码格式。
        l_name, l_code = locale.getdefaultlocale()

        # 查找是否存在该进程
        out = cmd('wmic process where name="'+filename +
                '" get processid,executablepath,name')

        # 找到进程对应的路径
        filepath = []
        for i in out.decode(l_code).split('\n'):
            if filename in i:
                filepath.append(i.split(filename)[
                                0].strip().replace('\\', '/').lower())
        filepath = list(set(filepath))
        print('查找进程:成功找到%d个' % len(filepath), filepath)

        #杀掉进程
        print('终止服务:终止所有在运行的'+filename+'服务')
        os.system("taskkill /F /IM "+filename)
        time.sleep(1)  # 等待终止服务运行完毕

        #删除文件
        for i in filepath:
            os.remove(i+filename)
            os.mkdir(i+filename)
            print('删除文件:删除并用同名文件夹占位 %s' % (i+filename))

        print('\n程序执行结束')
        os.system('pause')
    else:
        if sys.version_info[0] == 3:
            ctypes.windll.shell32.ShellExecuteW(
                None, "runas", sys.executable, __file__, None, 1)
        else:  # in python2.x
            ctypes.windll.shell32.ShellExecuteW(None, u"runas", unicode(
                sys.executable), unicode(__file__), None, 1)

    print('\n程序执行结束')


except Exception as e:
    print(e)
    print('\n异常中止')
    os.system('pause')
    

我的Chrome都在D盘,考虑到很多用户的Chrome都是在C盘的。
本来是想exe右键兼容赋予管理员权限的,结果发现网上有其他方法在运行的时候获取管理权限,所以借用了(参考文献:获取管理员权限 https://blog.csdn.net/qq_17550379/article/details/79006655)

写完代码后,直接用pyinstaller打包成exe

打包好的exe附下载地址:

链接: https://pan.baidu.com/s/1ZZ2fHiOak62Y8BNx-NWD3g 提取码: m6tg

如果有其他更方便的方法,欢迎留言。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章