Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit efe3604

Browse files
Merge pull request avinashkranjan#2840 from smty2018/filesortergui
File Sorter GUI
2 parents e73e279 + 8e6d8c7 commit efe3604

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)

AltStyle によって変換されたページ (->オリジナル) /