I want to know how to clean ALL temporary files by using python code/direction. I cannot look at the names of FILES. Just want to clean my Temp Files Cache
Thanks in advance!
I want to know how to clean ALL temporary files by using python code/direction. I cannot look at the names of FILES. Just want to clean my Temp Files Cache
Thanks in advance!
from __future__ import print_function
from contextlib import suppress
import os
import shutil
top_level_tempFolder = os.environ["TEMP"]
lof = os.listdir(top_level_tempFolder)
for f in lof:
with suppress(PermissionError, FileNotFoundError):
if os.path.isdir(os.path.join(top_level_tempFolder, f)):
shutil.rmtree(os.path.join(top_level_tempFolder, f))
else:
os.remove(os.path.join(top_level_tempFolder, f))
print("done")
# os.startfile(top_level_tempFolder)
Using this will clean any files and folders that Windows is able to remove from the temp folder; and will not halt for any errors associated with permissions or missing files.