2

I'm trying to run a script (with admin privileges) that deletes a specific directory (using shutil.rmtree). Now, some of its files raises the following error:

WindowsError: [Error 5] Access is denied

When I delete it using Windows Explorer, it succeeds with no errors.

How can I delete the whole folder using shutil.rmtree without the errors?

I found out that I can use win32api.SetFileAttributes(filepath, win32con.FILE_ATTRIBUTE_NORMAL) to a specific file which fixes the error issue.

Is there a way to do it for the whole folder, or is there another way to delete the whole folder with no errors that I'm missing here?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Drxxd
  • 1,860
  • 14
  • 34
  • 1
    Can you narrow down which file is causing the issue? Then you can inspect its properties in Explorer to see which attribute is the cause of this error. My money is on `FILE_ATTRIBUTE_READONLY`. If that is indeed the case: https://stackoverflow.com/questions/4829043/how-to-remove-read-only-attrib-directory-with-python-in-windows – Thomas Aug 15 '17 at 08:15
  • @Thomas yes.. It seems that this was the problem. Thanks!! – Drxxd Aug 15 '17 at 08:34
  • @Thomas Yeah.. it seems to be a duplicate.. I don't know how I didn't find this question earlier – Drxxd Aug 15 '17 at 08:36

0 Answers0