-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorter.py
71 lines (45 loc) · 1.75 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
import os
import shutil
import datetime
def modification_date(filename):
t = os.path.getctime(filename)
return datetime.datetime.fromtimestamp(t)
def is_folder_created(f_folder, pwd_folders):
for folder in pwd_folders:
if f_folder == folder:
return True
return False
def create_date_folder(f, pwd, date_folders):
creation_date = modification_date(f)
folder = f"{creation_date.month}-XX-{creation_date.year}"
if not is_folder_created(folder, date_folders):
os.mkdir(pwd + folder)
return True
return False
def determine_date_folder(f):
creation_date = modification_date(f)
return f"{creation_date.month}-XX-{creation_date.year}"
def create_ext_folder(f, pwd):
ext_folders = [f for f in os.listdir(
pwd) if os.path.isdir(os.path.join(pwd, f))]
folder = os.path.splitext(f)[1].upper()[1:] + " Folder"
if not is_folder_created(folder, ext_folders):
os.mkdir(pwd + folder)
return True
return False
def determine_ext_folder(f):
return os.path.splitext(f)[1].upper()[1:] + " Folder"
pwd = r"C:\Users\pschultz\Downloads" + "\\"
pwd_files = [f for f in os.listdir(
pwd) if os.path.isfile(os.path.join(pwd, f))]
date_folders = [f for f in os.listdir(
pwd) if os.path.isdir(os.path.join(pwd, f))]
for f in pwd_files:
file_dir = pwd
if create_date_folder(pwd + f, pwd, date_folders):
date_folders = [f for f in os.listdir(
pwd) if os.path.isdir(os.path.join(pwd, f))]
file_dir += determine_date_folder(pwd + f) + "\\"
create_ext_folder(pwd + f, file_dir)
file_dir += determine_ext_folder(pwd + f) + "\\"
shutil.move(pwd + f, file_dir + f)