shutil.rmtree fails on Windows with 'Access is denied'

Sridhar Ratnakumar :

In Python, when running shutil.rmtree over a folder that contains a read-only file, the following exception is printed:

 File "C:\Python26\lib\shutil.py", line 216, in rmtree
   rmtree(fullname, ignore_errors, onerror)
 File "C:\Python26\lib\shutil.py", line 216, in rmtree
   rmtree(fullname, ignore_errors, onerror)
 File "C:\Python26\lib\shutil.py", line 216, in rmtree
   rmtree(fullname, ignore_errors, onerror)
 File "C:\Python26\lib\shutil.py", line 216, in rmtree
   rmtree(fullname, ignore_errors, onerror)
 File "C:\Python26\lib\shutil.py", line 216, in rmtree
   rmtree(fullname, ignore_errors, onerror)
 File "C:\Python26\lib\shutil.py", line 216, in rmtree
   rmtree(fullname, ignore_errors, onerror)
 File "C:\Python26\lib\shutil.py", line 216, in rmtree
   rmtree(fullname, ignore_errors, onerror)
 File "C:\Python26\lib\shutil.py", line 221, in rmtree
   onerror(os.remove, fullname, sys.exc_info())
 File "C:\Python26\lib\shutil.py", line 219, in rmtree
   os.remove(fullname)
WindowsError: [Error 5] Access is denied: 'build\\tcl\\tcl8.5\\msgs\\af.msg'

Looking in File Properties dialog I noticed that af.msg file is set to be read-only.

So the question is: what is the simplest workaround/fix to get around this problem - given that my intention is to do an equivalent of rm -rf build/ but on Windows? (without having to use third-party tools like unxutils or cygwin - as this code is targeted to be run on a bare Windows install with Python 2.6 w/ PyWin32 installed)

Justin Peel :

Check this question out:

What user do python scripts run as in windows?

Apparently the answer is to change the file/folder to not be read-only and then remove it.

Here's onerror() handler from pathutils.py mentioned by @Sridhar Ratnakumar in comments:

def onerror(func, path, exc_info):
    """
    Error handler for ``shutil.rmtree``.

    If the error is due to an access error (read only file)
    it attempts to add write permission and then retries.

    If the error is for another reason it re-raises the error.

    Usage : ``shutil.rmtree(path, onerror=onerror)``
    """
    import stat
    if not os.access(path, os.W_OK):
        # Is the error an access error ?
        os.chmod(path, stat.S_IWUSR)
        func(path)
    else:
        raise

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Overwrite directory with shutil.rmtree and os.mkdir sometimes gives 'Access is denied' error

Using shutil.rmtree() in a recursive function (Windows 10)

windows: pip uninstallation fails with access denied

shutil.rmtree() clarification

shutil.rmtree() clarification

os.mkdir can fail with PermissionError after successful shutil.rmtree on Windows

QueryServiceObjectSecurity fails with access denied error

shutil.rmtree to remove files only?

Python mocking not working for shutil.rmtree

Will shutil.rmtree() check for the root directory?

python shutil.rmtree throwing errors

CreateFile over USB HID device fails with Access Denied (5) since Windows 10 1809

CreateMutex() fails with ERROR_ACCESS_DENIED when called by process spawned by Windows Service

OpenProcess() fails for some users with access denied

Creation of new project fails with "access denied"

Elasticsearch docker on travis fails with folder access denied

docker build fails with access denied or repository not exist

SSL Certificate Binding fails - Access is denied

pip install access denied on Windows

Access to the Path is denied in windows service

Is os.remove()+os.rmdir() better then shutil.rmtree()

python - remove directory and subfolders with shutil.rmtree - str object not callable

`shutil.rmtree` does not work on `tempfile.TemporaryDirectory()`

ansible 2.0.0.2 - shutil.rmtree error but the playbook success

How to return a value from onerror callback in shutil.rmtree

Windows Time not synchronizing - "Access is denied" error

Access denied after January 2018 Windows updates

Windows Registry access denied when administrator

Access denied, cmd move windows 7