-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileReorganizer.py
66 lines (49 loc) · 1.98 KB
/
FileReorganizer.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
import os
import collections
from pprint import pprint
EXT_AUDIO = ['mp3']
EXT_VIDEO = ['mp4', 'mkv']
EXT_IMAGE = ['jpeg', 'png', 'jpg']
EXT_DOC = ['pdf', 'txt', 'docs', 'docx']
EXT_COMP = ['zip', 'rar']
EXT_APP = ['dmg' , 'exe']
pathB = 'YOUR PATH HERE/'
BASE_PATH = os.path.expanduser(pathB)
DEST_DIRS = ['Downloads', 'Music', 'Images', 'Videos', 'Docs', 'Apps', 'others']
for d in DEST_DIRS:
dir_path = os.path.join(BASE_PATH, d)
if not os.path.isdir(dir_path):
os.makedirs(dir_path)
DOWNLOADS_PATH = os.path.join(BASE_PATH, 'Downloads')
files_mapping = collections.defaultdict(list)
files_list = os.listdir(DOWNLOADS_PATH)
pprint(files_mapping)
print(files_list)
for file_name in files_list:
if file_name[0] != '.':
file_ext = file_name.split('.')[-1]
files_mapping[file_ext].append(file_name)
pprint(files_mapping)
for f_ext, f_list in files_mapping.items():
if f_ext in EXT_IMAGE:
for file in f_list:
os.rename(os.path.join(DOWNLOADS_PATH, file), os.path.join(BASE_PATH, 'Images', file))
for f_ext, f_list in files_mapping.items():
if f_ext in EXT_VIDEO:
for file in f_list:
os.rename(os.path.join(DOWNLOADS_PATH, file), os.path.join(BASE_PATH, 'Videos', file))
elif f_ext in EXT_DOC:
for file in f_list:
os.rename(os.path.join(DOWNLOADS_PATH, file), os.path.join(BASE_PATH, 'Docs', file))
elif f_ext in EXT_COMP:
for file in f_list:
os.rename(os.path.join(DOWNLOADS_PATH, file), os.path.join(BASE_PATH, 'others', file))
elif f_ext in EXT_APP:
for file in f_list:
os.rename(os.path.join(DOWNLOADS_PATH, file), os.path.join(BASE_PATH, 'Apps', file))
elif f_ext in EXT_AUDIO:
for file in f_list:
os.rename(os.path.join(DOWNLOADS_PATH, file), os.path.join(BASE_PATH, 'Music', file))
else:
for file in f_list:
os.rename(os.path.join(DOWNLOADS_PATH, file), os.path.join(BASE_PATH, 'others', file))