-
Notifications
You must be signed in to change notification settings - Fork 0
/
FreeC.py
93 lines (82 loc) · 2.63 KB
/
FreeC.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
import shutil
import threading
# 获取系统变量
systemDrive = os.environ.get('systemdrive')
winDir = os.environ.get('windir')
userProfile = os.environ.get('userprofile')
systemDrive += '\\'
# 删除文件字典
filesDict = {
systemDrive:['.tmp', '._mp', '.log', '.gid',
'.chk', '.old'],
winDir:['.bak']
}
# 删除文件夹字典,只清空,不删除根文件
directoriesDict = {
systemDrive:['recycled'],
winDir:['prefetch', 'temp'],
userProfile:['cookies', 'recent',
'Local Settings\\Temporary Internet Files',
'Local Settings\\Temp']
}
print_lock = threading.Lock()
def printLock(message):
"""安全输出日志"""
with print_lock:
print(message)
def deleteFilesWithExtension(directory, extension):
"""
递归删除所有指定类型的文件
param:
directory: 遍历的文件夹 e.g. D:/demo
extension: 指定的文件类型 e.g. .txt
"""
for root, _, files in os.walk(directory):
# 遍历文件
for file in files:
# 检查文件后缀是否匹配
if file.endswith(extension):
filePath = os.path.join(root, file)
# 删除文件
try:
printLock(f"Deleting: {filePath}")
os.remove(filePath)
printLock(f"Deleted: {filePath}")
# 删除文件时异常
except OSError as e:
printLock(f"Error while deleting\n{filePath}\n{e}")
def deleteDirectory(directory):
"""清空文件夹"""
try:
printLock(f"Deleting: {directory}")
shutil.rmtree(directory)
printLock(f"Deleted: {directory}")
except Exception as e:
printLock(f"Error deleting {directory}:\n{e}")
# 线程池
threads = []
diry = 'C:\\Windows\\SoftwareDistribution\\Download\\'
a = threading.Thread(target=deleteDirectory,
args=(diry,))
threads.append(a)
# 添加删除文件线程
for path, exts in filesDict.items():
for ext in exts:
b = threading.Thread(target=deleteFilesWithExtension,
args=(path, ext))
threads.append(b)
# 添加删除文件夹线程
for path, directs in directoriesDict.items():
for direct in directs:
__path = os.path.join(path, direct)
c = threading.Thread(target=deleteDirectory,
args=(__path,))
threads.append(c)
if __name__ == '__main__':
for thread in threads:
thread.start()
for thread in threads:
thread.join()
print("Success free C space")
os.system('pause')