Advertisement
TTpocToXaKep

Temp files cleaning Windows 10

Feb 13th, 2023 (edited)
784
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. import os
  2. import shutil
  3. import subprocess
  4.  
  5. # Directories to search for temporary files
  6. temp_dirs = [r'C:\Windows\Temp',
  7.              r'C:\Users\<UserName>\AppData\Local\Temp'] # rewrite username on ur without <>
  8.  
  9. def delete_temp_files(dirs):
  10.     error_deleting = False
  11.     for temp_dir in temp_dirs:
  12.         for root, dirs, files in os.walk(temp_dir):
  13.             for file in files:
  14.                 file_path = os.path.join(root, file)
  15.                 try:
  16.                     os.remove(file_path)
  17.                 except Exception as e:
  18.                     error_deleting = True
  19.                     print(f'Error deleting {file_path}: {e}')
  20.                     subprocess.run(['explorer', root], check=True)
  21.     if not error_deleting:
  22.         print('Cleaning was successful!')
  23.         print('''
  24.        /\_/\
  25.       ( o.o )
  26.        > ^ <
  27.        ''')
  28.  
  29. if __name__ == '__main__':
  30.     delete_temp_files(temp_dirs)
  31.  
  32. # example of working
  33. # Cleaning was successful!
  34. #
  35. #        /\_/\
  36. #       ( o.o )
  37. #        > ^ <
  38. # guys if u find error - paste it in comment please i junior in python
  39.  
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement