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

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

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