[Python] 分享一个用python写的window清理缓存垃圾小程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# coding: utf-8
import os
import shutil
 
del_extension = ['.tmp', '._mp', '.log', '.gid', '.chk', '.old', '.xlk', '.bak']
del_userprofile = ['cookies', 'recent', 'Temporary Internet Files', 'Temp']
del_windir = ['prefetch', 'temp']
# 获取系统盘
root_sys_drive = os.environ['systemdrive'] + '\\'
# 获取用户目录
root_user_profile = os.environ['userprofile']
# 获取 Windows 目录
root_win_dir = os.environ['windir']
 
 
def del_dir_or_file(root):
    try:
        if os.path.isfile(root):
            # 删除文件
            os.remove(root)
            print ('file: ' + root + ' removed')
        elif os.path.isdir(root):
            # 删除文件夹
            shutil.rmtree(root)
            print ('directory: ' + root + ' removed')
    except WindowsError:
        print ('failure: ' + root + " can't remove")
 
for roots, dirs, files in os.walk(root_sys_drive, topdown=False):
    # 生成并展开以 root 为根目录的目录树,参数 topdown 设定展开方式从底层到顶层
    for every_file in files:
        # 获取文件扩展名
        file_extension = os.path.splitext(every_file)[1]
        if file_extension in del_extension:
            # 组合文件完整路径
            full_path = os.path.join(roots, every_file)
            # 调用函数删除文件
            del_dir_or_file(full_path)
for every_dir in del_userprofile:
    # 组合文件夹完整路径
    full_path = os.path.join(root_user_profile, every_dir)
    # 调用函数删除文件夹
    del_dir_or_file(full_path)
for every_dir in del_windir:
    full_path = os.path.join(root_win_dir, every_dir)
    del_dir_or_file(full_path)
print ('\nfinished')
上一篇: