-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_sorter.py
More file actions
107 lines (84 loc) · 3.16 KB
/
file_sorter.py
File metadata and controls
107 lines (84 loc) · 3.16 KB
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
import os
import sys
import file_info as f_info
from tkinter import *
from tkinter import ttk
MISC_FILENAME = "misc_file_list.txt"
misc_filenames = []
def get_dir_path() -> str:
"""Gets directory path to be sorted from second CLI argument."""
argv_len = len(sys.argv)
if argv_len == 2:
return sys.argv[1]
return ''
def print_dir_files(dir: str):
"""Outputs a list of all directory files sorted into their relating files."""
os.chdir(dir)
dir_filenames = os.listdir()
# Determine where each file falls under file groups
for filename in dir_filenames:
success = False
# Look at each file group
for file_group in f_info.file_groups:
# Try to add filename to file group
success = file_group.try_add_file(filename)
# Continue to next filename if success
if success:
break
# If no match for file group, add to misc filenames list
if not success:
misc_filenames.append(filename)
os.makedirs('moved_lists', exist_ok=True)
os.chdir('moved_lists')
print('Writing sorted filename lists!')
print('-' * 10)
# Output filename lists
for file_group in f_info.file_groups:
file_group.output_file_list()
print(MISC_FILENAME)
with open(MISC_FILENAME, 'w', encoding='utf-8') as file:
misc_filenames_str = '\n'.join(misc_filenames)
file.write(misc_filenames_str)
print('-' * 10)
def move_dir_files(dir: str, mode: int):
"""Moves directory files to corresponding folder."""
os.chdir(dir)
count = 0
for file_group in f_info.file_groups:
count += file_group.move_file_list(mode)
print(f"Total files sorted: {count}")
print(f"Couldn't move: {misc_filenames}")
def handle_sorting(dir: str, mode_list_result):
"""Handles the various sorting mode conditions."""
if mode_list_result != ():
print_dir_files(dir)
if mode_list_result[0] <= 1:
move_dir_files(dir, mode_list_result[0])
else:
print("Please select a mode first.")
def handle_gui():
"""Handles the graphical user interface window of the sorting system."""
sort = lambda: handle_sorting(dir_path.get(), mode_list.curselection())
window_root = Tk()
window_root.title('Python File Sorter')
py_logo = PhotoImage(file='assets/Python-logo.png')
window_root.iconphoto(False, py_logo)
frame = ttk.Frame(window_root, padding=10)
frame.grid()
Label(frame, text='Directory Path', padx=5, justify='center').grid(row=0, column=0)
dir_path = StringVar(value=get_dir_path())
dir_path_entry = Entry(frame, width=50, textvariable=dir_path)
dir_path_entry.grid(row=0, column=1)
Label(frame, text="Select a Mode", justify='center', pady=5).grid(row=1, column=0)
mode_list = Listbox(frame, height=3, width=12)
mode_list.grid(row=2, column=0)
options = ['Move Mode', 'Copy Mode', 'List Mode']
for i in options:
mode_list.insert(END, i)
submit_btn = Button(frame, text='Submit', command=sort)
submit_btn.grid(row=1, column=1)
window_root.mainloop()
def main():
handle_gui()
if __name__ == '__main__':
main()