在Windows上,shutil.rmtree失败,显示“访问被拒绝”

在Windows上,shutil.rmtree失败,显示“访问被拒绝”,第1张

在Windows上,shutil.rmtree失败,显示“访问被拒绝”

检查以下问题:

python脚本在Windows中以什么用户身份运行?

显然,答案是将文件/文件夹改为非只读,然后将其删除。

这是@Sridhar
Ratnakumar在评论中提到的

onerror()
处理程序
pathutils.py

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


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5642697.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存