【Python】报错处理笔记

lovewaits / 2023-08-29 / 原文

shutil.rmtree(path)

报错:PermissionError: [WinError 5]

分析:对应的目录或文件被设置了只读属性

解决方案:

def remove_readonly(func, path, _):  # 错误回调函数,改变只读属性位,重新删除
    "Clear the readonly bit and reattempt the removal"
    os.chmod(path, stat.S_IWRITE)
    func(path)

shutil.rmtree(path, onerror=remove_readonly)  # 设置错误回调函数οnerrοr=remove_readonly

 参考文章:https://blog.csdn.net/qq_21444067/article/details/131036512