Advertisement
Python253

clear_temp

Apr 19th, 2024 (edited)
728
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.12 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: clear_temp.py
  4. # Version: 1.0.1
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. This script is designed to find and optionally remove temporary files in Windows system's temporary directories.
  9.  
  10. Requirements:
  11. - Python 3.x installed on the system.
  12. - The script is intended to run on Windows due to its reliance on Windows environment variables.
  13.  
  14. Functions:
  15. - get_temp_directories(): Retrieves the temporary directories on the Windows system.
  16. - get_files_in_temp_directories(): Finds all files in the temporary directories.
  17. - print_files(files): Prints the files found in temporary directories.
  18. - remove_temp_files(): Prompts the user whether to remove all temporary files found. If the user agrees, it attempts to delete each file.
  19.  
  20. Usage:
  21. 1: Save the script as "clear_temp.py".
  22. 2: Run the script using Python.
  23. 3: Follow the prompts to view the temporary files and choose whether to delete them.
  24.  
  25. Additional Notes:
  26. - Be cautious when deleting temporary files, as some may be in use by running applications.
  27. - Deleting certain temporary files may cause unexpected behavior in applications that rely on them.
  28. - The script permanently deletes files; they are not moved to the system's Recycle Bin.
  29. """
  30.  
  31. import os
  32.  
  33. def get_temp_directories():
  34.     """
  35.    Retrieves the temporary directories on the Windows system.
  36.  
  37.    Returns:
  38.        list: A list of temporary directories.
  39.    """
  40.     temp_dirs = []
  41.     for temp_var in ['TEMP', 'TMP']:
  42.         if temp_var in os.environ:
  43.             temp_dirs.append(os.environ[temp_var])
  44.     return temp_dirs
  45.  
  46. def get_files_in_temp_directories():
  47.     """
  48.    Finds all files in the temporary directories.
  49.  
  50.    Returns:
  51.        list: A list of tuples containing file paths and sizes.
  52.    """
  53.     temp_dirs = get_temp_directories()
  54.     files = []
  55.     for temp_dir in temp_dirs:
  56.         for root, _, filenames in os.walk(temp_dir):
  57.             for filename in filenames:
  58.                 file_path = os.path.join(root, filename)
  59.                 file_size = os.path.getsize(file_path)
  60.                 files.append((file_path, file_size))
  61.     return files
  62.  
  63. def print_files(files):
  64.     """
  65.    Prints the files found in temporary directories.
  66.  
  67.    Args:
  68.        files (list): A list of tuples containing file paths and sizes.
  69.    """
  70.     if not files:
  71.         print("\nNo files found in temp directories.\n")
  72.     else:
  73.         print("\nFiles in temp directories:\n")
  74.         for file_path, file_size in files:
  75.             print(f"{file_path} - {file_size} bytes")
  76.  
  77. def remove_temp_files():
  78.     """
  79.    Prompts the user whether to remove all temporary files found.
  80.  
  81.    If the user agrees, it attempts to delete each file.
  82.    """
  83.     files = get_files_in_temp_directories()
  84.     print_files(files)
  85.     if files:
  86.         choice = input("\nDo you want to attempt to remove all temp files?\n\n1: Yes\n2: No\n\nMake your selection (1 or 2): ").strip().lower()
  87.         if choice == "1":
  88.             deleted_count = 0
  89.             for file_path, _ in files:
  90.                 if os.path.exists(file_path):  # Check if file exists before attempting to delete
  91.                     try:
  92.                         os.remove(file_path)
  93.                         deleted_count += 1
  94.                     except Exception as e:
  95.                         pass  # Ignore errors during deletion
  96.             if deleted_count == 1:
  97.                 files_text = "file"
  98.             else:
  99.                 files_text = "files"
  100.             print(f"\n- Deleted {deleted_count} {files_text}.\n")
  101.             not_deleted_count = len(files) - deleted_count
  102.             if not_deleted_count > 0:
  103.                 if not_deleted_count == 1:
  104.                     files_text = "file"
  105.                 else:
  106.                     files_text = "files"
  107.                 print(f"\n- {not_deleted_count} file(s) were not deleted due to being in use by the system.\n")
  108.                 print("\nYou can manually delete them with elevated permissions, but be cautious as it may lead to undesirable effects on the system.\n")
  109.         else:
  110.             print("\nNo files were deleted.\n")
  111.  
  112. remove_temp_files()
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement