-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathempty_folder_cleaner.py
176 lines (147 loc) · 4.74 KB
/
empty_folder_cleaner.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#code written by @ciphrox at github
#to run empty_folder_cleaner.py on android use pydroid3(any others may also work)
import os
import time
import platform as pf
# data
notPermissioned = ('Android/data', 'Android/obb')
sys = pf.system()
init = ''
rem = '/storage/emulated/0/'
fNum = allNum = 0
empty, notDel = [], []
startTime = 0
rmPre= lambda s: s.replace(rem,'')
# COLORS
RED = '\033[1;31;40m'
WHITE = '\033[0;38;48m'
INVERT = '\033[3;37;40m'
BRIGHT = '\033[1;37;40m'
#----------FUNCTIONS----------#
# Clears Screen
def cls():
if sys == 'Windows':
os.system('cls')
else:
os.system('clear')
# Check The given directory and sub directories
def checkDir(path, rem):
global fNum, allNum
try:
for item in os.listdir(path):
item = os.path.join(path, item)
allNum += 1
if os.path.isdir(item) and not item.endswith(notPermissioned) :
print(rmPre(item))
fNum += 1
if not os.listdir(item):
empty.append(rmPre(item))
else:
checkDir(item, rem)
checkBack(item,rem)
except:
pass
# Check the pervious directory
def checkBack(path,rem):
if all(rmPre(os.path.join(path,item)) in empty for item in os.listdir(path)):
empty.append(rmPre(path.replace(rem,'')))
# Starts again if error or wrong input
def startAgain(show=False):
cls()
if show:
print(BRIGHT+show, '\n')
start()
#Ending Info about files/folders.
def endInfo(string, dir, noDEL=True):
try:
if noDEL: noDEL=len(empty)
s = lambda num: "s" if num > 1 else ""
pad = lambda num: ' ' * \
(len(str(max(noDEL, fNum, allNum))) - len(str(num)) + 1)
showNums = lambda pad, n, pr: print('\n',BRIGHT, n, WHITE, pad(n), pr, sep='', end='')
showNums(pad, noDEL, f'Empty folder{s(len(empty))} {string}')
showNums(pad, fNum, f'Total folder{s(fNum)} Scanned.')
showNums(pad, allNum, f'Total file{s(allNum)} and folder{s(allNum)}.')
print(f'\n{BRIGHT}Scanned Folder:{WHITE}{dir}')
except:
input("Crashed")
# Show List
def showList(canDelete,dirList):
print(f'{BRIGHT}{canDelete} Folders:\n{WHITE}',end='')
listLen=len(dirList)
pad = lambda n : (len(str(listLen))-len(str(n)))*' '
for i in range(listLen):
item = dirList[i]
print(BRIGHT,i+1,')',pad(i+1),WHITE,item,sep='')
if not dirList: print("None")
#Delete
def delete(dir):
nDel=0
print(f'\n{BRIGHT}Deleted Folders:\n{WHITE}')
for i,e in enumerate(empty,1):
try:
path = init+e
os.rmdir(path)
nDel+=1
print(BRIGHT, i,') ',
WHITE, path, RED+'-----Removed!!!'+WHITE,sep='')
except:
notDel.append(path)
if not nDel: print("None Deleted")
showList('\nUnable to delete',notDel)
endInfo('Deleted!!',dir,nDel)
# Start
def start():
global init, startTime
global RED, WHITE, INVERT, BRIGHT
option = input(
f'{BRIGHT}Empty Folder Cleaner.\n\n\
Choose Your option:\n\
1.{WHITE} Check the empty folders and subfolders in curent path\n{BRIGHT}\
2.{WHITE} Check the empty folders and subfolders in given path by user\n\
Or write "exit" at any input for exiting.\n\
{INVERT}After checking you will be asked to enter (Y/N) if you want to delete the Folders.{WHITE}\n\
{BRIGHT}IF GETTING ANY CODE LIKE <03m[> ON TERMINAL THEN CHOOSE OPTION 3\n\n\
{BRIGHT}Enter your option:{WHITE} ').lower()
print()
if option == 'exit': return False
if option == '1':
path = os.getcwd()
elif option == '2':
print(f"{RED}NOTE ! - If given none then would scan the internal storage(Android).{WHITE}")
path = input(f'{BRIGHT}Enter path to scan and delete empty folders: {WHITE}')
if path == 'exit': return False
if sys == 'Linux': init = rem
if not os.path.exists(init+path):
startAgain('The Path does not exit.\nTry again')
return
if sys == 'Windows' and path.endswith('\\') and path.endswith('//'):
path += '\\'
elif option == '3':
RED = WHITE = INVERT = BRIGHT = ''
startAgain('')
return
else:
startAgain('Wrong Option')
return
startTime = time.time()
dirPath = init+path
checkDir(dirPath, rem)
cls()
showList('Empty',empty)
if empty:
toDel = input(f'\n{BRIGHT}Do you want to delete these?(Y/N): {WHITE}').upper()
if toDel=='Y':
delete(dirPath)
else:
endInfo('found.',dirPath)
return True
# Run
def main():
cls()
showTime=start()
if showTime:
_ = input(
f'\n\n{INVERT}[Took {round(time.time()-startTime,2)}s in executing]{WHITE} \nPress Enter to Exit.')
if __name__=="__main__":
main()