The Palos Publishing Company

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

Create a Python tool for digital spring cleaning

Here’s a practical Python script you can use as a Digital Spring Cleaning Tool. It helps identify and optionally clean up unused files, large files, empty folders, and even old downloads in a directory.

You can run this script from the command line, and it will guide you through a digital decluttering session.

python
import os import time from pathlib import Path import shutil # Configuration DOWNLOADS_DIR = str(Path.home() / "Downloads") AGE_THRESHOLD_DAYS = 180 SIZE_THRESHOLD_MB = 100 TARGET_DIRECTORY = str(Path.home()) def get_file_age_in_days(filepath): file_stat = os.stat(filepath) modification_time = file_stat.st_mtime return (time.time() - modification_time) / (60 * 60 * 24) def get_file_size_in_mb(filepath): return os.path.getsize(filepath) / (1024 * 1024) def find_large_files(directory, size_threshold_mb): print(f"nScanning for large files > {size_threshold_mb} MB...") for foldername, subfolders, filenames in os.walk(directory): for filename in filenames: filepath = os.path.join(foldername, filename) try: if get_file_size_in_mb(filepath) > size_threshold_mb: print(f"[LARGE] {filepath} - {get_file_size_in_mb(filepath):.2f} MB") except (OSError, PermissionError): continue def find_old_files(directory, age_threshold_days): print(f"nScanning for files not modified in the last {age_threshold_days} days...") for foldername, subfolders, filenames in os.walk(directory): for filename in filenames: filepath = os.path.join(foldername, filename) try: if get_file_age_in_days(filepath) > age_threshold_days: print(f"[OLD] {filepath} - {int(get_file_age_in_days(filepath))} days old") except (OSError, PermissionError): continue def find_empty_folders(directory): print("nScanning for empty folders...") for foldername, subfolders, filenames in os.walk(directory, topdown=False): if not subfolders and not filenames: print(f"[EMPTY] {foldername}") def clean_downloads(downloads_dir, age_threshold_days): print(f"nChecking Downloads folder: {downloads_dir}") for filename in os.listdir(downloads_dir): filepath = os.path.join(downloads_dir, filename) try: if os.path.isfile(filepath) and get_file_age_in_days(filepath) > age_threshold_days: print(f"[OLD DOWNLOAD] {filepath} - {int(get_file_age_in_days(filepath))} days old") except (OSError, PermissionError): continue def confirm_and_delete(prompt, paths): if not paths: return confirm = input(f"nDo you want to delete {len(paths)} items listed above? [y/N]: ").lower() if confirm == 'y': for path in paths: try: if os.path.isfile(path): os.remove(path) elif os.path.isdir(path): shutil.rmtree(path) print(f"Deleted: {path}") except Exception as e: print(f"Failed to delete {path}: {e}") else: print("No files were deleted.") def main(): print("🔧 Digital Spring Cleaning Tool 🔧") print(f"Target Directory: {TARGET_DIRECTORY}") # Collect paths for optional deletion old_files = [] large_files = [] empty_dirs = [] # Find old files print(f"n[1] Finding files older than {AGE_THRESHOLD_DAYS} days...") for foldername, subfolders, filenames in os.walk(TARGET_DIRECTORY): for filename in filenames: filepath = os.path.join(foldername, filename) try: if get_file_age_in_days(filepath) > AGE_THRESHOLD_DAYS: print(f"[OLD] {filepath}") old_files.append(filepath) except Exception: continue # Find large files print(f"n[2] Finding files larger than {SIZE_THRESHOLD_MB} MB...") for foldername, subfolders, filenames in os.walk(TARGET_DIRECTORY): for filename in filenames: filepath = os.path.join(foldername, filename) try: if get_file_size_in_mb(filepath) > SIZE_THRESHOLD_MB: print(f"[LARGE] {filepath}") large_files.append(filepath) except Exception: continue # Find empty folders print(f"n[3] Finding empty folders...") for foldername, subfolders, filenames in os.walk(TARGET_DIRECTORY, topdown=False): if not subfolders and not filenames: print(f"[EMPTY] {foldername}") empty_dirs.append(foldername) # Clean old files in Downloads print(f"n[4] Scanning Downloads folder ({DOWNLOADS_DIR}) for old files...") clean_downloads(DOWNLOADS_DIR, AGE_THRESHOLD_DAYS) # Confirm deletions confirm_and_delete("Old Files", old_files) confirm_and_delete("Large Files", large_files) confirm_and_delete("Empty Folders", empty_dirs) print("n✅ Spring cleaning completed!") if __name__ == "__main__": main()

Key Features:

  • Scans home directory for:

    • Large files

    • Old unused files

    • Empty folders

  • Scans Downloads folder separately for cleanup

  • Prompts for deletion with user confirmation

  • Modular functions for easy customization

Usage:

  1. Save the script to a file, e.g., spring_cleaner.py.

  2. Run it via terminal:

    nginx
    python spring_cleaner.py

This script is safe by default as it only deletes upon confirmation. You can adjust thresholds or automate cleanup further based on your preferences.

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