前提条件
安装Python3.10
直接下载连接:https://www.python.org/ftp/python/3.10.6/python-3.10.6-amd64.exe
如果无法下载,就去官方网站下载: https://www.python.org/downloads/windows/
新建clean.py文件
文件内容如下:
# 清理系统垃圾
import glob
import os
import winreg
def remove_files(rootdir, ext):
'''
清理rootdir目录下的符合的文件
'''
for i in glob.glob(os.path.join(rootdir, ext)):
try:
os.remove(i)
except Exception as e:
print(e)
def remove_reg_os(regs):
'''
清理系统注册表
'''
reg = winreg.CreateKey(
winreg.HKEY_LOCAL_MACHINE,
r'SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\MyComputer\NameSpace'
)
try:
for name in regs:
winreg.DeleteKey(reg, name)
except Exception as e:
print(e)
winreg.CloseKey(reg)
def remove_reg_user(regs):
'''
清理用户注册表
'''
reg = winreg.CreateKey(
winreg.HKEY_CURRENT_USER,
r'Software\Microsoft\Windows\CurrentVersion\Explorer\MyComputer\NameSpace'
)
try:
for name in regs:
winreg.DeleteKey(reg, name)
except Exception as e:
print(e)
winreg.CloseKey(reg)
if __name__ == "__main__":
#删除系统库
regs = [
r'{E701A357-F43B-42c9-98D1-96B6C11EAD87}',
r'{f86fa3ab-70d2-4fc7-9c99-fcbf05467f3a}',
r'{d3162b92-9365-467a-956b-92703aca08af}',
r'{B4BFCC3A-DB2C-424C-B029-7FE99A87C641}',
r'{3dfdf296-dbec-4fb4-81d1-6a3438bcf4de}',
r'{088e3905-0323-4b02-9826-5d99428e115f}',
r'{24ad3ad4-a569-4530-98e1-ab02f9417aa8}'
]
remove_reg_user(regs)
#删除默认文件夹(库)
systemdrive = os.getenv('systemdrive')
#清除系统盘下临时文件
remove_files(systemdrive, '*.tmp')
#清除系统盘下文件
remove_files(systemdrive, '*._mp')
#清除系统盘下日志文件
remove_files(systemdrive, '*.log')
#清除系统盘下文件
remove_files(systemdrive, '*.gid')
#清除系统盘下文件
remove_files(systemdrive, '*.chk')
#清除系统盘下旧文件
remove_files(systemdrive, '*.old')
windir = os.getenv('windir')
#清除系统目录下备份文件
remove_files(windir, '*.bak')
#清除系统目录下临时文件
remove_files(windir, '*.tmp')
#清除系统目录下临时文件夹
remove_files(os.path.join(windir, 'temp'), '*.*')
#清除系统目录下文件
remove_files(os.path.join(windir, 'prefetch'), '*.*')
systemroot = os.getenv('systemroot')
#清除系统目录下dump文件夹
remove_files(os.path.join(systemroot, 'minidump'), '*.*')
#清除系统目录下文件
remove_files(os.path.join(systemroot, 'prefetch'), '*.*')
allusersprofile = os.getenv('allusersprofile')
#清除软件数据
# remove_files(os.path.join(allusersprofile, 'Documents'), '*.*')
temp = os.getenv('temp')
#清除用户temp目录下文件
remove_files(temp, '*.*')
userprofile = os.getenv('userprofile')
#清除用户最近访问记录
remove_files(os.path.join(userprofile, 'Recent'), '*.*')
#清除用户最近office访问记录
remove_files(
os.path.join(userprofile,
'AppData\\Roaming\\Microsoft\\Office\\Recent'), '*.*')
#清除用户本地temp
remove_files(os.path.join(userprofile, 'AppData\\Local\\Temp'), '*.*')
#清除图标缓存
#关闭Windows外壳程序explorer
os.system(r'taskkill /f /im explorer.exe')
os.system(r'attrib -h -s -r "%userprofile%\AppData\Local\IconCache.db"')
os.system(r'del /f "%userprofile%\AppData\Local\IconCache.db"')
os.system(
r'attrib /s /d -h -s -r "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\*"'
)
os.system(
r'del /f "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\thumbcache_32.db"'
)
os.system(
r'del /f "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\thumbcache_96.db"'
)
os.system(
r'del /f "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\thumbcache_102.db"'
)
os.system(
r'del /f "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\thumbcache_256.db"'
)
os.system(
r'del /f "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\thumbcache_1024.db"'
)
os.system(
r'del /f "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\thumbcache_idx.db"'
)
os.system(
r'del /f "%userprofile%\AppData\Local\Microsoft\Windows\Explorer\thumbcache_sr.db"'
)
#清理 系统托盘记忆的图标
os.system(
r'echo y|reg delete "HKEY_CLASSES_ROOT\Local Settings\Software\Microsoft\Windows\CurrentVersion\TrayNotify" /v IconStreams'
)
os.system(
r'echo y|reg delete "HKEY_CLASSES_ROOT\Local Settings\Software\Microsoft\Windows\CurrentVersion\TrayNotify" /v PastIconsStream'
)
#重启Windows外壳程序explorer
os.system(r'start explorer.exe')
运行
开始-》运行-》输入cmd
回车打开命令行,
输入以下命令:
python clean.py
回车开始执行。