forked from ChuishenX/blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DStroyer.py
49 lines (44 loc) · 1.55 KB
/
DStroyer.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
import os
import time
import functools
"""
Usage:
1. Copy the script to the folder that you would like to scan.
2. Input "y" and the naughty MacOS files will perish.
"""
filelist = []
def clear(detection, path="."):
# dir_list = os.listdir(path) # No longer used because of its slowness. (AVG: 8s for each scan.)
# for i in dir_list:
# abspath = os.path.join(os.path.abspath(path), i)
# if os.path.isfile(abspath):
# if i.startswith(detection):
# print("Detected:", abspath)
# os.remove(abspath)
# if os.path.isdir(abspath):
# clear(detection, path=abspath)
for fileordir in os.scandir(path):
if fileordir.is_dir():
clear(detection, fileordir.path)
i = fileordir.path
abspath = os.path.join(os.path.abspath(path), i)
if os.path.isfile(abspath):
if i.startswith(detection):
print("Detected:", abspath)
filelist.append(i)
if __name__ == '__main__':
ret = input(f"Are you sure to delete all MacOS .DS_Store/._* files? [y/n] ")
if ret == 'y':
starttime = time.time()
clear(".DS_Store", path=".")
clear("._", path=".")
endtime = time.time()
print("Starting deleting...")
for delfile in filelist:
try:
os.remove(delfile)
print(f"Deleted: {delfile}")
except Exception as e:
print(f"Failed: {e}")
print(f"Time Cost: {endtime-starttime:.3f}")
print("Deletion completed.")