-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
81 lines (67 loc) · 1.95 KB
/
script.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
import os
from os import path
import datetime
print("Input path folder")
file_path = input()
if not os.path.exists(file_path):
print("Path not found")
exit()
files = []
dirs = [file_path]
def checkdir(folder):
dirs.pop(0)
content = os.listdir(folder)
for el in content:
fullname = os.path.join(folder, el)
if os.path.isfile(fullname):
files.append(fullname)
elif os.path.isdir(fullname):
dirs.append(fullname)
return dirs
def abs_name(files_arr):
names = []
for file in files_arr:
full_name = path.basename(file)
name = path.splitext(full_name)[0]
names.append(name)
names.sort()
return names
def dublicator(names_arr):
new_arr = []
for name in names_arr:
if "— копия" in name:
new_str = name.split(" — копия", 1)[0]
if new_str not in new_arr:
new_arr.append(new_str)
elif name not in new_arr:
new_arr.append(name)
return new_arr
def remove_files(folder):
dirs.pop(0)
content = os.listdir(folder)
for el in content:
fullname = os.path.join(folder, el)
if os.path.isfile(fullname):
full_name = path.basename(el)
name = path.splitext(full_name)[0]
if name not in end_arr:
deleted.append(fullname)
os.remove(fullname)
else:
end_arr.remove(name)
elif os.path.isdir(fullname):
dirs.append(fullname)
return dirs
while len(dirs) > 0:
checkdir(dirs[0])
end_arr = dublicator(abs_name(files))
filename = "deleted_"+str(datetime.date.today())+".txt"
file = open(filename, "w+")
deleted = []
dirs = [file_path]
while len(dirs) > 0:
remove_files(dirs[0])
for note in deleted:
file.write(note+'\n')
file.close()
print("Script ended success!")