Advertisement
Python253

take_out_the_trash

Apr 26th, 2024
904
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: take_out_the_trash.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. This script provides functionality to manage the recycle bin, including listing its contents and emptying it.
  9.  
  10. Requirements:
  11. - Python 3.x
  12. - Windows operating system
  13.  
  14. Functions:
  15. - list_trash(): Lists the contents of the recycle bin.
  16. - empty_trash(): Empties the recycle bin.
  17. - main(): Main function to execute the script.
  18.  
  19. Usage:
  20. 1. Ensure Python 3.x is installed on your system.
  21. 2. Run the script using the command: python take_out_the_trash.py
  22. 3. Follow the prompts to view recycle bin contents and empty it if desired.
  23.  
  24. Additional Notes:
  25. - The script uses ctypes and win32com modules, which are specific to Windows.
  26. - It prompts the user to confirm before emptying the recycle bin.
  27. - If the recycle bin is already empty, it displays an appropriate message.
  28. """
  29.  
  30. import ctypes
  31. from win32com.client import Dispatch
  32.  
  33. def list_trash():
  34.     """
  35.    Lists the contents of the recycle bin.
  36.    """
  37.     shell = Dispatch("Shell.Application")
  38.     trash = shell.Namespace(10)
  39.     items = trash.Items()
  40.     print("Contents:")
  41.     for item in items:
  42.         print(item.Name)
  43.  
  44. def empty_trash():
  45.     """
  46.    Empties the recycle bin.
  47.    """
  48.     # Define the SHEmptyRecycleBin function signature
  49.     SHEmptyRecycleBin = ctypes.windll.shell32.SHEmptyRecycleBinW
  50.     # Set flags for the function call
  51.     SHERB_NOCONFIRMATION = 0x1
  52.     dwFlags = SHERB_NOCONFIRMATION
  53.     # Call the SHEmptyRecycleBin function to empty the recycle bin
  54.     res = SHEmptyRecycleBin(None, None, dwFlags)
  55.     if res != 0:
  56.         # If the function call returns a non-zero value, handle the error
  57.         error_message = ctypes.FormatError(res)
  58.         print(f"\nError 0x{res:08x} - OSCAR THE GROUCH SAYS: {error_message}!\n\t- It seems the recycle bin was already empty -\n\n\tExiting Program...\tGoodBye!\n")
  59.         exit(res)
  60.     else:
  61.         print("\nThe trash has been taken out!\n")
  62.  
  63. def main():
  64.     """
  65.    Main function to execute the script.
  66.    """
  67.     # List contents of recycle bin
  68.     list_trash()
  69.    
  70.     # Ask user if they want to empty the recycle bin
  71.     choice = input("\nDo you want to take out the trash?\n\n1: Yes\n0: No\n\nMake your selection (1 or 0): ")
  72.     if choice == "1":
  73.         # Empty the recycle bin
  74.         empty_trash()
  75.     elif choice == "0":
  76.         print("\n\tNo action taken. Exiting Program...\tGoodBye!\n")
  77.     else:
  78.         print("\nInvalid choice. Please enter '1' or '0'.\n")
  79.  
  80. # Call the main function
  81. if __name__ == "__main__":
  82.     main()
  83.  
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement