Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import tkinter as tk
- from tkinter import filedialog
- import subprocess
- def search_files():
- search_path = entry_path.get()
- target_extension = entry_extension.get()
- if not os.path.isdir(search_path):
- result_text.delete(1.0, tk.END)
- result_text.insert(tk.END, "Invalid directory path")
- return
- results = []
- for root, dirs, files in os.walk(search_path):
- for file in files:
- if file.endswith(target_extension):
- results.append(os.path.join(root, file))
- if results:
- result_text.delete(1.0, tk.END)
- result_text.insert(tk.END, "\n".join(results))
- else:
- result_text.delete(1.0, tk.END)
- result_text.insert(tk.END, "No files found")
- def browse_folder():
- folder_path = filedialog.askdirectory()
- entry_path.delete(0, tk.END)
- entry_path.insert(0, folder_path)
- def save_results():
- file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text files", "*.txt")])
- if file_path:
- with open(file_path, "w") as file:
- file.write(result_text.get(1.0, tk.END))
- def open_file(event):
- index = result_text.index(tk.CURRENT)
- file_path = result_text.get(index+" linestart", index+" lineend")
- if os.path.exists(file_path):
- subprocess.Popen(["start", file_path], shell=True)
- # Create main window
- window = tk.Tk()
- window.title("Najeeb Advanced File Search")
- # Path Entry
- tk.Label(window, text="Search Path:").grid(row=0, column=0, sticky="w")
- entry_path = tk.Entry(window, width=50)
- entry_path.grid(row=0, column=1, padx=5, pady=5)
- tk.Button(window, text="Browse", command=browse_folder).grid(row=0, column=2, padx=5, pady=5)
- # Extension Entry
- tk.Label(window, text="Target Extension:").grid(row=1, column=0, sticky="w")
- entry_extension = tk.Entry(window)
- entry_extension.grid(row=1, column=1, padx=5, pady=5)
- # Search Button
- tk.Button(window, text="Search", command=search_files).grid(row=2, column=1, padx=5, pady=5)
- # Results Text Area with Scrollbar
- result_text = tk.Text(window, height=15, width=60)
- result_text.grid(row=3, column=0, columnspan=3, padx=5, pady=5)
- scrollbar = tk.Scrollbar(window, command=result_text.yview)
- scrollbar.grid(row=3, column=3, sticky="ns")
- result_text.config(yscrollcommand=scrollbar.set)
- # Bind double click event to open_file function
- result_text.bind("<Double-Button-1>", open_file)
- # Save Button
- tk.Button(window, text="Save Results", command=save_results).grid(row=4, column=1, padx=5, pady=5)
- # Run the application
- window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement