Skip to content

Commit 8e6d8c7

Browse files
authored
Add files via upload
1 parent 08b7bdd commit 8e6d8c7

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

File Sorter GUI/ReadMe.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
User select a directory and automatically organizes files by their extensions into separate subdirectories.
2+
Tkinter,os,shutil,filedialog:These libraries collectively enable the creation of the GUI, handling file operations, and user interaction
3+
4+
requirement:
5+
the libraries used in the project (os, shutil, and filedialog) are actually built-in Python libraries.

File Sorter GUI/app.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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

Comments
 (0)