-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDelsubdir.py
More file actions
112 lines (92 loc) · 4.43 KB
/
Delsubdir.py
File metadata and controls
112 lines (92 loc) · 4.43 KB
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
#Delsubdir v1.0
import os
import fnmatch
import shutil
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
#######################################################################################
#Directory input Function
#######################################################################################
def Delsubdir():
print("Enter the Directory to Customize")
folder_with_path=input()
if os.path.isdir(folder_with_path):
print(f"{bcolors.OKGREEN}"+f"{bcolors.BOLD}\n"+folder_with_path + f"{bcolors.OKGREEN} is a valid Folder{bcolors.ENDC}")
print(f"{bcolors.BOLD}")
choice = input("""
*****************************MAIN MENU*****************************
1: Delete file in directory based on file extension
2: Delete sub-directory based on pattern
3: Delete the blank sub-directory
4: Quit
*******************************************************************
Please enter your choice: """+f"{bcolors.ENDC}")
#######################################################################################
# File Deletion based on Extension
#######################################################################################
if choice == '1':
try:
count=0
print("\nEnter the Extension to delete the file(s)")
file_extension=input()
for parent, dirnames, filenames in os.walk(folder_with_path):
for fn in filenames:
if fn.lower().endswith(file_extension):
os.remove(os.path.join(parent, fn))
print("\nThe Deleting File(s) list\n"+fn)
count+=1
print("\n"+str(count)+" files deleted!")
except:
print("\nNo File with this Extension. Thank You")
#######################################################################################
# Sub-Directory Deletion based on Pattern
#######################################################################################
elif choice == '2':
try:
count=0
print("\nEnter the Pattern to delete the sub-directory(s)")
pattern=input()
for parent, dirnames, filenames in os.walk(folder_with_path):
if fnmatch.fnmatch(parent,pattern):
print("\nThe Deleting Sub-Directory(s) list\n"+parent)
shutil.rmtree(parent)
count+=1
print("\n"+str(count)+" sub-directory(s) deleted!")
except:
print("\nNo Sub-Directory with this Pattern. Thank You")
#######################################################################################
# Blank Sub-Directory Deletion
#######################################################################################
elif choice == '3':
try:
count=0
for parent, dirnames, filenames in os.walk(folder_with_path):
if len(os.listdir(parent)) == 0:
print("\nThe Deleting Sub-Directory(s) list\n"+parent)
shutil.rmtree(parent)
count+=1
print("\n"+str(count)+" blank sub-directory(s) deleted!")
except:
print("\nNo Blank Sub-Directory. Thank You")
#######################################################################################
# Exit
#######################################################################################
elif choice=='4':
exit()
else:
print("Please select the correct number")
print("Please try again\n\n")
else:
print ("\nDirectory not exists. Try Again!!")
#######################################################################################
# Main
#######################################################################################
if __name__ == '__main__':
Delsubdir()