-
Notifications
You must be signed in to change notification settings - Fork 0
/
folder_manager_functions.py
147 lines (107 loc) · 5.59 KB
/
folder_manager_functions.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
import os, shutil, re
from collections import Counter
# list of extensions (memory based)
image_files=['.jpg','.jpeg','.png','.gif', '.bmp', '.svg','.jfif', '.dwg']
text_and_document_files=['.pdf', '.txt', '.docx', '.doc', '.odt', '.ics']
compressed_files=['.zip', '.rar', '.7z', '.jar', '.gz', '.xz', '.deb', '.arc', '.arj']
video_files=['.mp4', '.m4p', '.m4v', '.mpv', '.mp2', '.webm','.mkv']
executable_files=['.exe', '.run', '.sh', '.desktop', '.out', '.bin','.msi']
presentation_files=['.ppt', '.pptx', '.odp']
spreadsheet_files=['.xls', 'xlsx','xlsb', '.xlsm', '.ods', '.sdc', 'sxc']
database_files=['.db', '.csv', '.dbf', '.json']
script_files=['.py','.c', '.js', '.cpp', '.css', '.html', '.jsp', '.xml',
'.jspx', '.cs', '.dart', '.swift', '.kt', '.kts', '.ktm']
disk_file=['.iso']
audio_file=['.wav', '.mp3']
def ext_arrange(arr):
dirr= arr[0]
if os.path.isdir(dirr):
#separating files
img=list(filter(lambda x: os.path.splitext(x)[1] in image_files, os.listdir(dirr) ))
documents_and_plaintext=list(filter(lambda x: os.path.splitext(x)[1] in text_and_document_files, os.listdir(dirr) ))
archives=list(filter(lambda x: os.path.splitext(x)[1] in compressed_files, os.listdir(dirr) ))
videos=list(filter(lambda x: os.path.splitext(x)[1] in video_files, os.listdir(dirr) ))
executables=list(filter(lambda x: os.path.splitext(x)[1] in executable_files, os.listdir(dirr) ))
presentations=list(filter(lambda x: os.path.splitext(x)[1] in presentation_files, os.listdir(dirr) ))
spreadsheets=list(filter(lambda x: os.path.splitext(x)[1] in spreadsheet_files, os.listdir(dirr) ))
databases=list(filter(lambda x: os.path.splitext(x)[1] in database_files, os.listdir(dirr) ))
scripts=list(filter(lambda x: os.path.splitext(x)[1] in script_files, os.listdir(dirr) ))
iso=list(filter(lambda x: os.path.splitext(x)[1] in disk_file, os.listdir(dirr) ))
audio=list(filter(lambda x: os.path.splitext(x)[1] in audio_file, os.listdir(dirr) ))
separation={'pictures': img,
'Docs':documents_and_plaintext,
'Compressed':archives,
'Videos':videos,
'executables': executables,
'presentations': presentations,
'Spreadsheets': spreadsheets,
'Database_Files': databases,
'Scripts': scripts,
'iso': iso,
'audios': audio
}
for folder in separation:
if not os.path.exists(os.path.join(dirr, folder)):
os.makedirs(os.path.join(dirr,folder))
new_folder=os.path.join(dirr, folder)
try:
for file in separation[folder]:
shutil.move(os.path.join(dirr, file),new_folder )
except:
print("Try running the same command again")
else:
print('your specified directory: {} is not a valid directory path on your machine'.format(dirr))
def name_arrange(arr):
dirr= arr[0]
if os.path.isdir(dirr):
# list initials of all file names
temp=list(map(lambda x: re.split(r'[\d\s_@#%&\'"*-]', x)[0], os.listdir(dirr)))
all_files= [ os.path.splitext(x.lower())[0] for x in temp]
alpha=Counter(re.findall(r'\w+', ' '.join(all_files)))
same= [ x for x in alpha if alpha[x]>1]
for key in same:
# print(key)
if not os.path.exists(os.path.join(dirr, key)):
os.makedirs(os.path.join(dirr, key))
new_folder=os.path.join(dirr, key)
for file in os.listdir(dirr):
try:
if os.path.join(dirr,new_folder) in os.path.splitext(os.path.join(dirr,file.lower()))[0] :
shutil.move(os.path.join(dirr, file), new_folder)
except:
pass
def deep_arrange(arr):
mode, path=arr
assert os.path.isdir(path)
if mode=='1':
# print('called deep arrange in mode {}'.format(mode))
name_arrange(os.path.splitext(path))
for sub_path in os.listdir(path):
if os.path.isdir(os.path.join(path,sub_path)):
ext_arrange(os.path.splitext(os.path.join(path,sub_path)))
elif mode=='2':
ext_arrange(os.path.splitext(path))
for sub_path in os.listdir(path):
if os.path.isdir(os.path.join(path,sub_path)):
name_arrange(os.path.splitext(os.path.join(path,sub_path)))
else:
print('{} is not a valid mode. \n python folder_manager.py -h'.format(mode))
def compress(arr):
kind, target=arr
assert os.path.isdir(target)
# archives to have the same name as the original file or directory
arc=os.path.splitext(target)[0]
if kind=='xztar':
print('this might take some time. xztar is the slowest archiving technique')
shutil.make_archive(arc,'xztar',target)
print('created an archive file. Now you can delete the original file')
elif kind=='gztar':
print('this might take some time')
shutil.make_archive(arc,'gztar',target)
print('created an archive file. Now you can delete the original file')
elif kind=='zip':
print('this might take some time. zip is the fastest archiving technique')
shutil.make_archive(arc,'zip',target)
print('created an archive file. Now you can delete the original file')
else:
print('archive type {} is either not supported by by your Operating System or not available')