Advertisement
Najeebsk

SEARCH-DIR.py

Feb 28th, 2024
799
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.58 KB | None | 0 0
  1. import os
  2. import tkinter as tk
  3. from tkinter import filedialog
  4. import subprocess
  5.  
  6. def search_files():
  7.     search_path = entry_path.get()
  8.     target_extension = entry_extension.get()
  9.  
  10.     if not os.path.isdir(search_path):
  11.         result_text.delete(1.0, tk.END)
  12.         result_text.insert(tk.END, "Invalid directory path")
  13.         return
  14.  
  15.     results = []
  16.     for root, dirs, files in os.walk(search_path):
  17.         for file in files:
  18.             if file.endswith(target_extension):
  19.                 results.append(os.path.join(root, file))
  20.  
  21.     if results:
  22.         result_text.delete(1.0, tk.END)
  23.         result_text.insert(tk.END, "\n".join(results))
  24.     else:
  25.         result_text.delete(1.0, tk.END)
  26.         result_text.insert(tk.END, "No files found")
  27.  
  28. def browse_folder():
  29.     folder_path = filedialog.askdirectory()
  30.     entry_path.delete(0, tk.END)
  31.     entry_path.insert(0, folder_path)
  32.  
  33. def save_results():
  34.     file_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text files", "*.txt")])
  35.     if file_path:
  36.         with open(file_path, "w") as file:
  37.             file.write(result_text.get(1.0, tk.END))
  38.  
  39. def open_file(event):
  40.     index = result_text.index(tk.CURRENT)
  41.     file_path = result_text.get(index+" linestart", index+" lineend")
  42.     if os.path.exists(file_path):
  43.         subprocess.Popen(["start", file_path], shell=True)
  44.  
  45. # Create main window
  46. window = tk.Tk()
  47. window.title("Najeeb Advanced File Search")
  48.  
  49. # Path Entry
  50. tk.Label(window, text="Search Path:").grid(row=0, column=0, sticky="w")
  51. entry_path = tk.Entry(window, width=50)
  52. entry_path.grid(row=0, column=1, padx=5, pady=5)
  53. tk.Button(window, text="Browse", command=browse_folder).grid(row=0, column=2, padx=5, pady=5)
  54.  
  55. # Extension Entry
  56. tk.Label(window, text="Target Extension:").grid(row=1, column=0, sticky="w")
  57. entry_extension = tk.Entry(window)
  58. entry_extension.grid(row=1, column=1, padx=5, pady=5)
  59.  
  60. # Search Button
  61. tk.Button(window, text="Search", command=search_files).grid(row=2, column=1, padx=5, pady=5)
  62.  
  63. # Results Text Area with Scrollbar
  64. result_text = tk.Text(window, height=15, width=60)
  65. result_text.grid(row=3, column=0, columnspan=3, padx=5, pady=5)
  66. scrollbar = tk.Scrollbar(window, command=result_text.yview)
  67. scrollbar.grid(row=3, column=3, sticky="ns")
  68. result_text.config(yscrollcommand=scrollbar.set)
  69.  
  70. # Bind double click event to open_file function
  71. result_text.bind("<Double-Button-1>", open_file)
  72.  
  73. # Save Button
  74. tk.Button(window, text="Save Results", command=save_results).grid(row=4, column=1, padx=5, pady=5)
  75.  
  76. # Run the application
  77. window.mainloop()
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement