Eryk Sun <[email protected]> added the comment:
> The doc on rmtree states
> Exceptions raised by onerror will not be caught.
> Does this mean I can't use try/exept inside of onerro
rmtree() does not call onerror() in a try/except statement. An exception raised
in onerror() will propagate to the scope that called rmtree().
The documentation has an example onerror() handler for Windows readonly files:
import os, stat
import shutil
def remove_readonly(func, path, _):
"Clear the readonly bit and reattempt the removal"
os.chmod(path, stat.S_IWRITE)
func(path)
shutil.rmtree(directory, onerror=remove_readonly)
I'd check whether the exception and function are expected values. For example:
import os, stat
import shutil
def remove_readonly(func, path, exc_info):
"Clear the readonly bit and reattempt the removal"
# ERROR_ACCESS_DENIED = 5
if func not in (os.unlink, os.rmdir) or exc_info[1].winerror != 5:
raise exc_info[1]
os.chmod(path, stat.S_IWRITE)
func(path)
shutil.rmtree(directory, onerror=remove_readonly)
----------
nosy: +eryksun
type: crash -> behavior
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue43657>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com