-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_sorter.py
45 lines (36 loc) · 1.58 KB
/
file_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
import os
import shutil
import json
class Sorter:
@staticmethod
def start_sort():
try:
with open('path/to/File Sorter/config.json', 'r') as config_file:
config = json.load(config_file)
source = config.get("source-dir")
if not source:
print("Source directory not specified in config.")
return
extension_config = config.get("file-extension")
os.chdir(source)
dest_folders = config.get("dest-folders", {})
for file in os.listdir():
file_classifier = extension_config.get(os.path.splitext(file)[1])
folder = dest_folders.get(file_classifier)
if file_classifier is None:
print(f"{file} cannot be moved due to unspecified type")
continue
if folder is None:
print(f"No destination folder specified for {file_classifier} files.")
continue
source_path = os.path.join(source, file)
dest_path = os.path.join(folder, file)
shutil.move(source_path, dest_path)
except FileNotFoundError:
print("Config file not found.")
except json.JSONDecodeError:
print("Error decoding JSON.")
except Exception as e:
print(f"An error occurred: {e}")
finally:
print("Program has been executed")