How to delete a folder in python? rmtree onerror

I recently had this problem: I needed a function that completely deletes a folder in windows, so I searched, and this is what I get:

How to delete / delete a folder that is not empty using Python? empty-with-python

Answers that look normal seem a bit confusing and big for me ... there should be a better way to solve the file access problem in windows using shutil.rmtree (raise an error while trying to access read-only files) ...

+2
source share
1 answer

I want to share a simple way that works for me.

I just created a function that changes the write permission mode of a file and then deletes it using os.remove :

 import stat # needed for file stat def remShut(*args): func, path, _ = args # onerror returns a tuple containing function, path and exception info os.chmod(path, stat.S_IWRITE) os.remove(path) 

then when using rmtree add it to the onerror parameter:

 import shutil shutil.rmtree(desiredpath, onerror = remShut) 

Hope this helps someone with the same issue as me.

+4
source

All Articles