关于使用shutil.rmtree删除git文件夹时出现拒绝访问的问题
简介
在实际项目中发现,当使用shutil.rmtree
删除整个git目录时会出现.git文件无法删除的情况,报错是拒绝访问,原因是默认情况下.git文件是只读的,无法直接对其进行操作。
解决方法
将在删除文件时出现问题时,使用回调处理删除的异常,授予git文件相应权限即可,使用如下
import os
import stat
def delete(func, path, execinfo):
os.chmod(path, stat.S_IWUSR)
func(path)
rmtree(path, onerror=delete)