Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: clear_temp.py
- # Version: 1.0.1
- # Author: Jeoi Reqi
- """
- This script is designed to find and optionally remove temporary files in Windows system's temporary directories.
- Requirements:
- - Python 3.x installed on the system.
- - The script is intended to run on Windows due to its reliance on Windows environment variables.
- Functions:
- - get_temp_directories(): Retrieves the temporary directories on the Windows system.
- - get_files_in_temp_directories(): Finds all files in the temporary directories.
- - print_files(files): Prints the files found in temporary directories.
- - remove_temp_files(): Prompts the user whether to remove all temporary files found. If the user agrees, it attempts to delete each file.
- Usage:
- 1: Save the script as "clear_temp.py".
- 2: Run the script using Python.
- 3: Follow the prompts to view the temporary files and choose whether to delete them.
- Additional Notes:
- - Be cautious when deleting temporary files, as some may be in use by running applications.
- - Deleting certain temporary files may cause unexpected behavior in applications that rely on them.
- - The script permanently deletes files; they are not moved to the system's Recycle Bin.
- """
- import os
- def get_temp_directories():
- """
- Retrieves the temporary directories on the Windows system.
- Returns:
- list: A list of temporary directories.
- """
- temp_dirs = []
- for temp_var in ['TEMP', 'TMP']:
- if temp_var in os.environ:
- temp_dirs.append(os.environ[temp_var])
- return temp_dirs
- def get_files_in_temp_directories():
- """
- Finds all files in the temporary directories.
- Returns:
- list: A list of tuples containing file paths and sizes.
- """
- temp_dirs = get_temp_directories()
- files = []
- for temp_dir in temp_dirs:
- for root, _, filenames in os.walk(temp_dir):
- for filename in filenames:
- file_path = os.path.join(root, filename)
- file_size = os.path.getsize(file_path)
- files.append((file_path, file_size))
- return files
- def print_files(files):
- """
- Prints the files found in temporary directories.
- Args:
- files (list): A list of tuples containing file paths and sizes.
- """
- if not files:
- print("\nNo files found in temp directories.\n")
- else:
- print("\nFiles in temp directories:\n")
- for file_path, file_size in files:
- print(f"{file_path} - {file_size} bytes")
- def remove_temp_files():
- """
- Prompts the user whether to remove all temporary files found.
- If the user agrees, it attempts to delete each file.
- """
- files = get_files_in_temp_directories()
- print_files(files)
- if files:
- 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()
- if choice == "1":
- deleted_count = 0
- for file_path, _ in files:
- if os.path.exists(file_path): # Check if file exists before attempting to delete
- try:
- os.remove(file_path)
- deleted_count += 1
- except Exception as e:
- pass # Ignore errors during deletion
- if deleted_count == 1:
- files_text = "file"
- else:
- files_text = "files"
- print(f"\n- Deleted {deleted_count} {files_text}.\n")
- not_deleted_count = len(files) - deleted_count
- if not_deleted_count > 0:
- if not_deleted_count == 1:
- files_text = "file"
- else:
- files_text = "files"
- print(f"\n- {not_deleted_count} file(s) were not deleted due to being in use by the system.\n")
- print("\nYou can manually delete them with elevated permissions, but be cautious as it may lead to undesirable effects on the system.\n")
- else:
- print("\nNo files were deleted.\n")
- remove_temp_files()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement