Repository navigation
Expand file tree
/
Copy pathfile_sorter.py
More file actions
100 lines (81 loc) · 2.94 KB
/
Copy pathfile_sorter.py
File metadata and controls
100 lines (81 loc) · 2.94 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
import tkinter as tk
from tkinter import filedialog,Text,Frame,Button,INSERT,END,WORD,BOTH,LEFT,RIGHT,TRUE,Checkbutton,Listbox,Scrollbar,Y
from pathlib import Path
import shutil
selected_folder = None
global file_ex
def select_dir():
global selected_folder
selected_folder = filedialog.askdirectory()
if selected_folder:
l.config(text=f"Selected folder: {selected_folder}")
def organize_files():
if selected_folder:
target_dir = Path(selected_folder)
is_preview = preview.get() # Get checkbox value
sort_files(selected_folder, target_dir, is_preview)
def dynamic_lw(listbox,files):
longest = max(files,key=len)
n_width = len(longest)
listbox.config(width = n_width)
def sort_files(dir,target_dir,preview,):
file_types = {
".pdf" : "PDF's",
".exe" : "Applications",
".zip" : "Zip_files",
".jpeg": "Images",
".png" : "Images",
".jpg" : "Images",
".xlsx": "Excel_Files",
".docx": "Word_Documents",
".txt" :"Text_Files"
}
scroll = Scrollbar(canvas)
preview_listbox = Listbox(canvas, relief=tk.RIDGE, yscrollcommand=scroll.set)
scroll.config(command=preview_listbox.yview)
if preview:
scroll.pack(side=RIGHT, fill=Y)
preview_listbox.pack(fill=BOTH, expand=True)
preview_listbox.delete(0, tk.END) # Clear previous results
for file in target_dir.iterdir():
if file.is_file():
file_ex = file.suffix.lower()
if file_ex not in file_types:
if preview:
preview_listbox.insert(tk.END, f"{file_ex} is not supported")
else:
if preview:
preview_listbox.insert(tk.END, f"Would move: {file.name} -> {file_types[file_ex]}")
else:
folder_path = Path(dir) / file_types[file_ex]
folder_path.mkdir(exist_ok=True)
shutil.move(file, folder_path)
canvas = tk.Tk()
canvas.geometry("300x250")
canvas.title("File Sorter")
frame = tk.Frame(master=canvas, relief=tk.RIDGE, borderwidth=5)
frame.pack(padx=10,pady=5)
frame.config()
l= tk.Label(master=frame,text = "Slected folder : No folder is sleected")
l.pack(in_=frame)
b1 = Button(master=frame , text="Select",command = select_dir)
b1.pack()
Checkbutton1 = tk.IntVar()
preview = tk.IntVar()
Button1 = Checkbutton(canvas, text = "Log",
variable = Checkbutton1,
onvalue = 1,
offvalue = 0,
height = 2,
width = 10)
Button2 = Checkbutton(canvas, text = "Preview",
variable = preview,
onvalue = 1,
offvalue = 0,
height = 2,
width = 10)
b2= Button(canvas, text="Organize", command = organize_files)
Button1.pack()
Button2.pack()
b2.pack()
canvas.mainloop()