-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
folder_organizer.py
111 lines (80 loc) · 3.2 KB
/
folder_organizer.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
import shutil
import json
import sys
import os
from tkinter import Tk
from tkinter import filedialog
from typing import List
from typing import Dict
from typing import Tuple
from typing import Union
from typing import Optional
from typing import Any
def get_data() -> Tuple[str, Dict[str, str], List[str]]:
with open(os.path.join(base_path, 'data/last_path.txt'), 'r') as fp:
last_path: str = fp.read()
with open(os.path.join(base_path, 'data/formats.json'), 'r') as fp:
extensions: Dict[str, str] = json.load(fp)
with open(os.path.join(base_path, 'data/exclude.txt'), 'r') as fp:
exclude: List[str] = fp.read().strip().splitlines()
return last_path, extensions, exclude
def get_path(last_path: str) -> Union[Tuple[()], str]:
Tk().withdraw()
path: Union[Tuple[()], str] = filedialog.askdirectory(initialdir=last_path)
return path
def save_path(path: str) -> None:
with open(os.path.join(base_path, 'data/last_path.txt'), 'w') as fp:
fp.write(path)
def move_file(file: Any, new_path: str) -> None:
try:
dst = f'{new_path}/{file.name}'
shutil.move(file.path, dst)
except Exception:
pass
def check_folder(new_path: str) -> None:
if not os.path.isdir(new_path):
os.mkdir(new_path)
def check_extension(file: Any) -> Optional[str]:
_, file_ext = os.path.splitext(file.name)
for type_, ext in extensions.items():
if file_ext.lower().strip('.') in ext:
return type_
return None
if __name__ == '__main__':
base_path: str = os.path.abspath('.')
if getattr(sys, 'frozen', False):
base_path = sys._MEIPASS # type: ignore
last_path, extensions, exclude = get_data()
# Use path arg if present
path: Any = get_path(last_path)
# Continue if user selects a folder
if path != tuple():
# List files in directory
for file in os.scandir(path):
# Only move files that are not in excluded.txt
if file.is_file() and file.name not in exclude:
# If it is a file, correlate its extension
type_: Optional[str] = check_extension(file)
# Do not move unknown files
if not type_:
print(f'ERROR Unknown format from file: {file.path !r}')
else:
# Set destination to specific folder
new_path: str = f'{path}/{type_}'
# Create folder if necessary
check_folder(new_path)
# Move the file to the new folder
move_file(file, new_path)
# Move directories to a new folder
elif file.is_dir() and file.name not in extensions:
folder_name = 'Folder'
# Only move previously existing folders
if file.name != folder_name:
# Set destination to the newly folder
new_path = f'{path}/{folder_name}'
# Create folder if necessary
check_folder(new_path)
# Move the file to the new folder
move_file(file, new_path)
# Save last selected path
save_path(path)