Ticker

6/recent/ticker-posts

Make Temp Folder Cleaner Tool using Python Programming

Sabhi priya pathakon ko mera namaskar, Aj k is article mein hum sikhenge ek windows based application banana, ek esi application jise run karke ap apne Windows OS ke %temp% folder mai available Temporary files ko delete/clean kar sakte hai aur system ki performance boost kar sakte hai. Ye application hum python programming ki help se banayenge jiska pura code apko is article mein niche diya gaya hai, sath hi puri programming mein har steps ko humne detail mein samjhaya hai.
        Agar ap python programming ko use karna nahi jante to initial steps ko follow karne k liye jo video hum attach kar rahe hai use dekhein, or fir niche diye gaye code ka use karke program banayein.



%Temp% File Cleaner Program Coding:

  import os
import shutil
import platform
import tkinter as tk
from tkinter import messagebox

def clean_temp_folder():
    if platform.system() == 'Windows':
        temp_folder = os.environ.get('TEMP')
        if temp_folder:
            files = os.listdir(temp_folder)
            deleted_files = 0
            errors = []
            for file in files:
                try:
                    file_path = os.path.join(temp_folder, file)
                    if os.path.isfile(file_path):
                        os.remove(file_path)
                        deleted_files += 1
                    elif os.path.isdir(file_path):
                        shutil.rmtree(file_path)
                        deleted_files += 1
                except Exception as e:
                    errors.append((file, str(e)))
            if deleted_files > 0:
                messagebox.showinfo("Cleanup Complete", f"Deleted {deleted_files} files/folders from {temp_folder}")
            if errors:
                error_message = "\n".join([f"{file}: {error}" for file, error in errors])
                messagebox.showwarning("Errors", f"Failed to delete the following files/folders:\n{error_message}")
        else:
            messagebox.showwarning("Warning", "Unable to retrieve TEMP folder path.")
    else:
        messagebox.showinfo("Information", "This script is intended for Windows operating system.")

def clean_temp_gui():
    clean_temp_folder()

# Create main window
root = tk.Tk()
root.title("%Temp% Cleaner")

# Set window size and center it
window_width = 300
window_height = 100
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x_coordinate = (screen_width - window_width) // 2
y_coordinate = (screen_height - window_height) // 2
root.geometry(f"{window_width}x{window_height}+{x_coordinate}+{y_coordinate}")
root.resizable(False, False)

# Button to initiate cleanup
clean_button = tk.Button(root, text="Clean Temp Folder", command=clean_temp_gui)
clean_button.pack(padx=20, pady=20)

# Status bar with smaller font size and centered text
status_bar = tk.Label(root, text="This App is Designed and Created by Buzz2Day Tech. Copyright @2024", bd=1, relief=tk.SUNKEN, anchor=tk.CENTER, justify=tk.CENTER, font=("Arial", 8))
status_bar.pack(side=tk.BOTTOM, fill=tk.X)

# Run the application
root.mainloop()



Step by Step Explanation:

1. Imports:

import os
import shutil
import platform
import tkinter as tk
from tkinter import messagebox


Yeh sabse pehla kadam hai. Hum libraries ko import kar rahe hain jo humein file aur folder operations aur GUI banane mein madad karenge. os aur shutil libraries file aur directories ke sath kaam karne ke liye hoti hai, platform library system ka operating system maloom karne ke liye istemal hoti hai, tkinter library GUI banane ke liye hai, aur messagebox library popup messages banane ke liye hoti hai.


2. clean_temp_folder() Function:

  def clean_temp_folder():
    if platform.system() == 'Windows':
        temp_folder = os.environ.get('TEMP')
        if temp_folder:
            files = os.listdir(temp_folder)
            deleted_files = 0
            errors = []
            for file in files:
                try:
                    file_path = os.path.join(temp_folder, file)
                    if os.path.isfile(file_path):
                        os.remove(file_path)
                        deleted_files += 1
                    elif os.path.isdir(file_path):
                        shutil.rmtree(file_path)
                        deleted_files += 1
                except Exception as e:
                    errors.append((file, str(e)))
            if deleted_files > 0:
                messagebox.showinfo("Cleanup Complete", f"Deleted {deleted_files} files/folders from {temp_folder}")
            if errors:
                error_message = "\n".join([f"{file}: {error}" for file, error in errors])
                messagebox.showwarning("Errors", f"Failed to delete the following files/folders:\n{error_message}")
        else:
            messagebox.showwarning("Warning", "Unable to retrieve TEMP folder path.")
    else:
        messagebox.showinfo("Information", "This script is intended for Windows operating system.")

  


Yeh function %temp% folder ko saaf karne ke liye responsible hai. Sabse pehle, yeh check karta hai ki hum Windows operating system par hai ya nahi (platform.system() == 'Windows'). Phir, os.environ.get('TEMP') se temporary folder ka path fetch kiya jata hai. Uske baad, sabhi files aur folders ko delete kiya jata hai. Agar koi file ya folder delete nahi ho paata hai, toh errors ko handle kiya jata hai aur user ko bataya jata hai.


3. clean_temp_guit() Function:

    def clean_temp_gui():
    clean_temp_folder()
    


Yeh function GUI ke button par click hone par clean_temp_folder() function ko call karta hai.


4. Create Main Window:

      root = tk.Tk()
root.title("%Temp% Cleaner")
      

Yahan, ek naya Tkinter window banaya jata hai aur uska title set kiya jata hai.


5. Set Window Size and Center it:

window_width = 300
window_height = 100
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x_coordinate = (screen_width - window_width) // 2
y_coordinate = (screen_height - window_height) // 2
root.geometry(f"{window_width}x{window_height}+{x_coordinate}+{y_coordinate}")
root.resizable(False, False)

Yahan, hum neeche di gayi cheezon ko karte hain:

  • window_width aur window_height: Window ka size set kiya jata hai. Yahan pe, humne window ko 300x100 pixels ka size diya hai.
  • screen_width aur screen_height: Screen ka width aur height maloom kiya jata hai.
  • x_coordinate aur y_coordinate: Window ke coordinates ko calculate kiya jata hai taki wo screen ke center mein aa sakein.
  • root.geometry(f"{window_width}x{window_height}+{x_coordinate}+{y_coordinate}"): geometry() method ke zariye window ka size aur position set kiya jata hai.
  • root.resizable(False, False): Is line se humne window ko resize karne ki permission band ki hai, yani user window ka size change nahi kar sakta.

Is step se, humne window ko ek fixed size aur center mein position kiya hai.


6. Button to Initiate Cleanup:

  clean_button = tk.Button(root, text="Clean Temp Folder", command=clean_temp_gui)
clean_button.pack(padx=20, pady=20)

  


Yahan, humne ek button banaya hai jo %temp% folder ko saaf karne ke liye hai. Is button ka text "Clean Temp Folder" hai. command parameter ke through humne clean_temp_gui() function ko associate kiya hai, jisse button par click karne par clean_temp_folder() function ko call kiya jayega.


7. Status Bar:

    status_bar = tk.Label(root, text="This App is Designed and Created by Buzz2Day Tech. Copyright @2024", bd=1, relief=tk.SUNKEN, anchor=tk.W, justify=tk.CENTER, font=("Arial", 8))
status_bar.pack(side=tk.BOTTOM, fill=tk.X)

    


Yeh status bar neeche set ki gayi hai. Ismein ek choti si label hai jo application ki information ko display karti hai. Text ko center-aligned karne ke liye justify=tk.CENTER parameter ka istemal kiya gaya hai.


8. Run the Application:

root.mainloop()
Yeh method Tkinter application ko run karta hai aur GUI ko display karta hai. Jab tak yeh method chalta rahta hai, GUI interactable rahti hai.


Yeh tha poori coding! Jab aap isko run karte hain, ek chhota sa window khulega jismein "Clean Temp Folder" naam ka ek button hoga. Jab aap is button par click karenge, to %temp% folder ko saaf karne ka process shuru ho jayega. Agar koi files ya folders saaf nahi ho paayein, toh errors ka message box aayega. Neeche status bar mein application ki information bhi di gayi hai.


I hope yeh explanation aapke liye helpful hai. Agar kuch aur samajhna ho, toh Comment Karte rahiye!

Video Tutorial ke Liye Hamara Youtube Channel Jaroor Subscribe Karein:

Subscribe Now

एक टिप्पणी भेजें

0 टिप्पणियाँ