[New-bugs-announce] [issue43657] shutil.rmtree fails on readonly files in Windows, onerror not called

Walter White report at bugs.python.org
Mon Mar 29 08:52:29 EDT 2021


New submission from Walter White <homerun4711 at gmail.com>:

shutil.rmtree fails on readonly files in Windows.

Usually people are using the onerror callback to handle file permissions and retry, but that is not possible in this case because it is not triggerd.

onerror is only triggered if a OSError is found. 
In my case the unlink throws a PermissionError

Code shutil.rmdir():

try:
    os.unlink(fullname)
except OSError:
    onerror(os.unlink, fullname, sys.exc_info())


Traceback:


Traceback (most recent call last):

  File "c:\Users\user\test.py", line 121, in <module>
    shutil.rmtree(shutil.rmtree(working_dir),
  File "C:\python-3.9.1.amd64\lib\shutil.py", line 740, in rmtree
    return _rmtree_unsafe(path, onerror)
  File "C:\python-3.9.1.amd64\lib\shutil.py", line 613, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "C:\python-3.9.1.amd64\lib\shutil.py", line 613, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "C:\python-3.9.1.amd64\lib\shutil.py", line 618, in _rmtree_unsafe
    onerror(os.unlink, fullname, sys.exc_info())
  File "C:\python-3.9.1.amd64\lib\shutil.py", line 616, in _rmtree_unsafe
    os.unlink(fullname)
	
PermissionError: [WinError 5] Access denied: 'C:\\Users\\user\\somefile.txt'

os.stat:

st_mode=33060
st_ino=34621422136837665
st_dev=3929268297
st_nlink=1
st_uid=0
st_gid=0

----------
components: Library (Lib)
messages: 389697
nosy: homerun4711
priority: normal
severity: normal
status: open
title: shutil.rmtree fails on readonly files in Windows, onerror not called
type: crash
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43657>
_______________________________________


More information about the New-bugs-announce mailing list