-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorter.py
96 lines (89 loc) · 4.96 KB
/
sorter.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
import os
import shutil
import time
files = os.listdir()
years = []
months = []
days = []
type = ['Audio', 'Images', 'Videos']
audio = [".mp3", ".wav", ".m4a", ".flac"]
images = [".jpeg", ".png", ".jpg", ".gif", ".psd"]
videos = [".mp4", ".mkv", ".3gp", ".avi", ".mov", ".webm", ".ts"]
type_sort = {'Audio': audio, 'Images': images, 'Videos': videos}
month_day = {'Jan': [], 'Feb': [], 'Mar': [], 'Apr': [], 'May': [], 'Jun': [], 'Jul': [], 'Aug': [], 'Sep': [], 'Oct': [], 'Nov': [], 'Dec': []}
print("\nFile Sorter 3.0 by Marcko")
print('This will sort the files by filetype, year, months, and day of modification.')
print('Please note that this script will only work on the files on the same directory with it.\n')
transfer_mode = int(input('Would you like to copy or move your files?\n1.Copy\n2.Move\n'))
print("\nChecking files...")
for f in files:
mod_date = time.ctime(os.path.getmtime(f))
mod_date = mod_date.split(" ")
if mod_date[4] not in years:
years.append(mod_date[4])
if mod_date[1] not in months:
months.append(mod_date[1])
if mod_date[2] not in month_day[mod_date[1]] and mod_date[2] != '':
month_day[mod_date[1]].append(mod_date[2])
print("Making folders and proceeding to the file trasnfer...\n")
for f in files:
mod_date = time.ctime(os.path.getmtime(f))
mod_date = mod_date.split(" ")
if f != 'File Sorter 3.py' and f not in type and f != 'Others':
for t in type_sort:
for s in type_sort[t]:
if f.endswith(s):
if t not in os.listdir():
os.mkdir('./' + t)
for y in years:
if mod_date[4] == y:
if str(y) not in os.listdir('./' + t):
os.mkdir('./' + t + '/' + str(y))
for m in months:
if mod_date[1] == m:
if m not in os.listdir('./' + t + '/' + y):
os.mkdir('./' + t + '/' + y + '/' + m)
for d in month_day[m]:
if mod_date[2] == d:
if str(d) not in os.listdir('./' + t + '/' + y + '/' + m):
os.mkdir('./' + t + '/' + y + '/' + m + '/' + str(d))
if f not in os.listdir('./' + t + '/' + y + '/' + m + '/' + str(d)):
if transfer_mode == 1:
shutil.copy(f, './' + t + '/' + y + '/' + m + '/' + str(d))
print(f + ' copied!')
break
if transfer_mode == 2:
shutil.move(f, './' + t + '/' + y + '/' + m + '/' + str(d))
print(f + ' moved!')
break
else:
if transfer_mode == 1:
print('The file ' + f + ' will not be copied to avoid further duplication')
if transfer_mode == 2:
print('The file ' + f + ' is already in the folder, renaming...')
g = f.replace('.', '(1).') # nice
os.rename(f, g)
shutil.move(g, './' + t + '/' + y + '/' + m + '/' + str(d))
print(g + ' moved!')
if f in os.listdir():
if 'Others' not in os.listdir():
os.mkdir('./Others')
if transfer_mode == 1:
name, ext = os.path.splitext('./' + f)
if ext not in audio + videos + images:
print('The file ' + f + ' will not be copied to Others to avoid further duplication')
if transfer_mode == 2:
if f not in os.listdir('./Others'):
shutil.move(f, './Others')
print(f + " was moved to to Others")
else:
print('The file ' + f + ' is already in the folder, renaming...')
g = f.replace('.', '(1).')
os.rename(f, g)
shutil.move(g, './Others')
print(g + " was moved to to Others")
if transfer_mode == 1:
print("\nCopying has finished!\n")
if transfer_mode == 2:
print("\nMoving has finished!\n")
os.system('pause')