Best Python GUI File Manager – Automate Your File Management! π₯
Demo :
Click Video πππ
Code :
import os
import shutil
import tkinter as tk
from tkinter import filedialog, messagebox
# Function to browse file
def browse_file():
file_path = filedialog.askopenfilename()
entry_path.delete(0, tk.END)
entry_path.insert(0, file_path)
# Function to delete file
def delete_file():
file_path = entry_path.get()
if os.path.exists(file_path):
os.remove(file_path)
messagebox.showinfo("Success", "File Deleted Successfully")
else:
messagebox.showerror("Error", "File Not Found")
# Fixed Rename Function
def rename_file():
file_path = entry_path.get()
new_name = entry_new_name.get().strip()
if not os.path.exists(file_path):
messagebox.showerror("Error", "File Not Found")
return
if not new_name:
messagebox.showerror("Error", "Enter a New Name")
return
# Get directory and file extension
dir_name, old_filename = os.path.split(file_path)
ext = os.path.splitext(old_filename)[1] # Extract extension
# Create full new path
new_path = os.path.join(dir_name, new_name + ext)
try:
os.rename(file_path, new_path)
messagebox.showinfo("Success", f"File Renamed to {new_name + ext}")
entry_path.delete(0, tk.END)
entry_path.insert(0, new_path)
except Exception as e:
messagebox.showerror("Error", f"Rename Failed: {e}")
# Function to move file
def move_file():
file_path = entry_path.get()
new_location = filedialog.askdirectory()
if os.path.exists(file_path) and new_location:
shutil.move(file_path, new_location)
messagebox.showinfo("Success", "File Moved Successfully")
else:
messagebox.showerror("Error", "Invalid File or Location")
# GUI Setup
root = tk.Tk()
root.title("Python File Manager")
root.geometry("400x350")
tk.Label(root, text="Select File:").pack()
entry_path = tk.Entry(root, width=50)
entry_path.pack()
tk.Button(root, text="Browse", command=browse_file).pack()
tk.Label(root, text="New Name (without extension):").pack()
entry_new_name = tk.Entry(root, width=30)
entry_new_name.pack()
tk.Button(root, text="Delete File", command=delete_file, bg="red", fg="white").pack(pady=5)
tk.Button(root, text="Rename File", command=rename_file, bg="blue", fg="white").pack(pady=5)
tk.Button(root, text="Move File", command=move_file, bg="green", fg="white").pack(pady=5)
root.mainloop()
Comments
Post a Comment