forked from prashantsengar/ArrangePy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrange.py
More file actions
119 lines (101 loc) · 3.35 KB
/
arrange.py
File metadata and controls
119 lines (101 loc) · 3.35 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
111
112
113
114
115
116
117
118
119
#arrange.py
#Arranges the fiels according to their types for later classification
#uses shutil, os
import os
import shutil
FOLDER_TYPES = {'pPDF':['pdf'],
'Pimages':['png','jpeg','jpg','gif', 'tiff', 'psd', 'ico'],
'Pvideos':['mp4','mkv','avi','3gp'],
'Paudios':['mp3','wav'],
'Pprograms':['exe', 'app', 'out'],
'Pdocs':['xlsx','doc','xlsx','pptx','csv','txt','ppt', 'odt', 'rtf', 'ods', 'txt', 'pps']
}
RESULT_DIR = 'CleanedPy'
def identifyType(ext):
'''
Accept extenssion Example .pdf .mp4 and
return a category type from FOLDER_TYPES Dictionary
'''
for key,value in FOLDER_TYPES.items():
if ext[1:] in value:
return key
break
else:
return None
def makeFolders(lst):
'''
Accept A List of Folder name and
create that category name folder in RESULT_DIR
'''
if os.path.exists(RESULT_DIR) is False:
os.mkdir(RESULT_DIR)
for name in lst:
if name in os.listdir(RESULT_DIR):
return
os.mkdir(os.path.join(RESULT_DIR,name))
def moveFiles(src,dst):
'''
Accept source and destination and move files .
Return True if File is Copied other wise False
'''
res = True
try:
pass
shutil.move(src,os.path.join(RESULT_DIR,dst))
except:
res = False
return res
#Create Output and category folder if not Exists
makeFolders(FOLDER_TYPES.keys())
def startProcess(folder,file):
'''
Accept file name and parent folder_name(folder)
Return a Tuple(TRUE|FALSE,TYPE_OF_FILE)
'''
types = os.path.splitext(file)[1].lower()
src = os.path.join(folder,file)
dst = identifyType(types)
if dst is not None:
return moveFiles(src,dst),dst
return False,'Others(Not_moved)'
def strong_arrange():
TOTAL_COUNT={}
for foldername, subfolders, filenames in os.walk(folder):
for file in filenames:
if os.path.isfile(os.path.join(folder,file)):
status,types = startProcess(folder,file)
if types in TOTAL_COUNT:
TOTAL_COUNT[types] = TOTAL_COUNT[types]+1
else:
TOTAL_COUNT[types] = 1
return TOTAL_COUNT
def arrange():
TOTAL_COUNT = {}
for file in os.listdir(folder):
if os.path.isfile(os.path.join(folder,file)):
status,types = startProcess(folder,file)
if types in TOTAL_COUNT:
TOTAL_COUNT[types] = TOTAL_COUNT[types]+1
else:
TOTAL_COUNT[types]=1
return TOTAL_COUNT
if __name__ == '__main__':
print("Arrange files")
folder=os.getcwd()
print(folder)
choice = int(input("Press 1 for Weak arrange\nPress 2 for Strong arrange\n0 to exit\noption:"))
if choice == 1:
res = arrange()
if choice == 2:
res = strong_arrange()
if choice == 0:
exit(0)
#Final Result
message = "Result"
others="Others(Not_moved)"
print(f'{message:*^30s}')
for key,value in res.items():
if key == others:
continue
print(f'{value} file moved into Category {os.path.join(RESULT_DIR,key)}')
print(f'{res[others]} file Not moved')