I want to make my code delete all the content of the temporary and prefetch folders, however some of the files are read-only, so the code returns an error 5: access denied.
def deletetemp():
try:
for root2, dirs, files in os.walk(r'C:\Windows\Temp'):
for f in files:
os.unlink(os.path.join(root2, f))
for d in dirs:
shutil.rmtree(os.path.join(root2, d))
for root2, dirs, files in os.walk(r'C:\Windows\Prefetch'):
for f in files:
os.unlink(os.path.join(root2, f))
for d in dirs:
shutil.rmtree(os.path.join(root2, d))
for root2, dirs, files in os.walk(r'C:\Users\*myusername*\AppData\Local\Temp'):
for f in files:
os.unlink(os.path.join(root2, f))
for d in dirs:
shutil.rmtree(os.path.join(root2, d))
x.configure(text="Temporary/Prefetch files deleted!")
except Exception as e:
x.configure(text=str(e))
Exception in Tkinter callback
Traceback (most recent call last):
File "C:/Users/*myusername*/.PyCharmCE2019.1/config/scratches/scratch.py", line 10, in deletetemp
os.unlink(os.path.join(root2, f))
PermissionError: [WinError 5] Access is denied: 'C:\\Windows\\Temp\\vcredist_x86.exe'
This is the place I got the code from.
If you didn't notice yet, I'm a noob in coding. Please try and provide simple answers (though, of course, don't do the code entirely for me). And also, I did look myself for an answer, but failed finding one. If there is one somewhere, please just redirect me! I'll take a look at it and try and make something out of it.