|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +import tkinter as tk |
| 4 | +from tkinter import filedialog |
| 5 | + |
| 6 | +class file_sorter: |
| 7 | + def __init__(self, root): |
| 8 | + self.root = root |
| 9 | + self.root.title("File Sorter") |
| 10 | + self.root.configure(bg="#333") |
| 11 | + |
| 12 | + self.select_button = tk.Button(root, text="Select Directory", command=self.select_directory, bg="#555", fg="white") |
| 13 | + self.select_button.pack(pady=10) |
| 14 | + |
| 15 | + self.sort_button = tk.Button(root, text="Sort Files", command=self.sort_files, bg="#555", fg="white") |
| 16 | + self.sort_button.pack(pady=5) |
| 17 | + |
| 18 | + self.status_label = tk.Label(root, text="", bg="#333", fg="white") |
| 19 | + self.status_label.pack(pady=10) |
| 20 | + |
| 21 | + def select_directory(self): |
| 22 | + self.dir_path = filedialog.askdirectory() |
| 23 | + self.status_label.config(text="Selected directory: " + self.dir_path) |
| 24 | + |
| 25 | + def sort_files(self): |
| 26 | + if hasattr(self, "dir_path"): |
| 27 | + for filename in os.listdir(self.dir_path): |
| 28 | + if os.path.isfile(os.path.join(self.dir_path, filename)): |
| 29 | + ext = filename.split(".")[-1] |
| 30 | + target_dir = os.path.join(self.dir_path, ext) |
| 31 | + |
| 32 | + if not os.path.exists(target_dir): |
| 33 | + os.makedirs(target_dir) |
| 34 | + |
| 35 | + source_path = os.path.join(self.dir_path, filename) |
| 36 | + target_path = os.path.join(target_dir, filename) |
| 37 | + |
| 38 | + shutil.move(source_path, target_path) |
| 39 | + |
| 40 | + self.status_label.config(text="Files sorted successfully.") |
| 41 | + else: |
| 42 | + self.status_label.config(text="Please select a directory first.") |
| 43 | + |
| 44 | +if __name__ == "__main__": |
| 45 | + root = tk.Tk() |
| 46 | + app = file_sorter(root) |
| 47 | + root.mainloop() |
| 48 | + |
0 commit comments