1

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.

melpomene
  • 84,125
  • 8
  • 85
  • 148
CritZ
  • 13
  • 5
  • Did you see this: https://stackoverflow.com/questions/4829043/how-to-remove-read-only-attrib-directory-with-python-in-windows? – Denis Rasulev Jun 29 '19 at 16:20

2 Answers2

4

As mentioned here try to change the permissions for the file using os.chmod(path_, stat.S_IWRITE) and remove it

nag
  • 749
  • 4
  • 9
  • Thanks! I managed to make it work. However, I actually got the code from the comment of Denis Rasulev (who commented on my question), I found a code snipplet that made it work. I have a different problem now however, but this one is lifted! – CritZ Jun 29 '19 at 17:19
  • Of course, readonly is a file attribute, not a permission. It's annoying that Python copied this weird behavior from MSVC, which goes all the way back to C for MS-DOS in the 1980s. If we have an [i]mmutable file in a Linux filesystem, we can't make it writable via `chmod`. Yay for complete lack of consistency. – Eryk Sun Jun 30 '19 at 02:25
0

Thanks! It worked for me. Make sure to import stat first

import stat

os.chmod(path_, stat.S_IWRITE)

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 11 '22 at 22:27