The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Create a Python bot to clean your desktop

Here’s a Python bot script that automatically organizes and cleans your desktop by moving files into categorized folders based on their extensions:

python
import os import shutil from datetime import datetime # Define your desktop path desktop_path = os.path.join(os.path.expanduser("~"), "Desktop") # Define folder categories based on file extensions file_categories = { 'Images': ['.png', '.jpg', '.jpeg', '.gif', '.bmp', '.svg', '.webp'], 'Documents': ['.pdf', '.doc', '.docx', '.txt', '.xls', '.xlsx', '.ppt', '.pptx'], 'Archives': ['.zip', '.rar', '.7z', '.tar', '.gz'], 'Scripts': ['.py', '.js', '.sh', '.bat', '.rb'], 'Executables': ['.exe', '.msi', '.apk'], 'Media': ['.mp3', '.mp4', '.wav', '.mkv', '.mov', '.avi'], 'Others': [] } # Create category folders if they don't exist def create_category_folders(): for category in file_categories: folder_path = os.path.join(desktop_path, category) if not os.path.exists(folder_path): os.makedirs(folder_path) # Move files to appropriate folders def clean_desktop(): files = [f for f in os.listdir(desktop_path) if os.path.isfile(os.path.join(desktop_path, f))] for file_name in files: file_path = os.path.join(desktop_path, file_name) file_ext = os.path.splitext(file_name)[1].lower() moved = False for category, extensions in file_categories.items(): if file_ext in extensions: target_folder = os.path.join(desktop_path, category) shutil.move(file_path, os.path.join(target_folder, file_name)) moved = True break if not moved: other_folder = os.path.join(desktop_path, 'Others') shutil.move(file_path, os.path.join(other_folder, file_name)) # Add timestamped log def log_cleaning(): log_path = os.path.join(desktop_path, "cleaning_log.txt") with open(log_path, "a") as log_file: log_file.write(f"Desktop cleaned at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}n") def main(): create_category_folders() clean_desktop() log_cleaning() print("Desktop cleaning complete.") if __name__ == "__main__": main()

How it works:

  • It categorizes common file types.

  • Creates folders if they don’t exist.

  • Moves files based on their extension.

  • Logs each clean-up with a timestamp.

You can run this script manually or schedule it using a task scheduler (e.g., Windows Task Scheduler or cron on macOS/Linux). Want help setting it to run automatically?

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About