The Palos Publishing Company

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

Python Script to Clean Up Your Downloads Folder

Keeping your Downloads folder organized can be a tedious task, especially when files accumulate rapidly from different sources like browsers, email attachments, or other apps. Automating the cleanup process using a Python script can save time and reduce clutter effectively.

Here’s a Python script designed to clean up your Downloads folder by sorting files into categorized subfolders based on their file types. It can move documents, images, videos, archives, and more into dedicated folders automatically.

python
import os import shutil from pathlib import Path # Set your downloads folder path here DOWNLOADS_FOLDER = Path.home() / "Downloads" # Define file extensions categories FILE_CATEGORIES = { "Images": [".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tiff", ".svg", ".webp"], "Videos": [".mp4", ".mkv", ".flv", ".avi", ".mov", ".wmv", ".webm"], "Documents": [".pdf", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".txt", ".odt", ".ods"], "Archives": [".zip", ".rar", ".tar", ".gz", ".7z", ".bz2"], "Audio": [".mp3", ".wav", ".aac", ".flac", ".ogg"], "Scripts": [".py", ".js", ".sh", ".bat", ".pl"], "Executables": [".exe", ".msi", ".app", ".dmg"], "Others": [] } def create_folders(): for folder in FILE_CATEGORIES.keys(): folder_path = DOWNLOADS_FOLDER / folder if not folder_path.exists(): folder_path.mkdir() def move_files(): for item in DOWNLOADS_FOLDER.iterdir(): if item.is_file(): moved = False for category, extensions in FILE_CATEGORIES.items(): if item.suffix.lower() in extensions: destination = DOWNLOADS_FOLDER / category / item.name shutil.move(str(item), destination) moved = True break if not moved: # Move to Others if no category matched destination = DOWNLOADS_FOLDER / "Others" / item.name shutil.move(str(item), destination) def main(): create_folders() move_files() print("Downloads folder cleaned and files sorted.") if __name__ == "__main__": main()

How This Script Works:

  1. Folder Path Setup: It sets your Downloads folder path dynamically by detecting your home directory.

  2. Categories Definition: Different file extensions are grouped into categories like Images, Videos, Documents, etc.

  3. Folder Creation: It creates these category folders inside Downloads if they don’t already exist.

  4. File Movement: Each file in Downloads is checked against the extension lists. Files are moved into the matching folder.

  5. Unmatched Files: Files with extensions not listed are moved into an “Others” folder.

Usage:

  • Save the script as clean_downloads.py.

  • Run it using Python (Python 3.x recommended):

    bash
    python clean_downloads.py
  • The script will organize your Downloads folder immediately.

Customization Tips:

  • You can add or remove file extensions from the FILE_CATEGORIES dictionary as needed.

  • Modify DOWNLOADS_FOLDER if your downloads are in a different location.

  • Extend functionality by adding file size or date-based filtering if required.

This Python script provides a straightforward and automated way to keep your Downloads folder tidy without manual sorting.

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