Python shutil.rmtree:无法删除win7上的git目录
windows 7 prof 64位上python 2.7的简单测试用例: 我通过python签出目录中的git项目,比如说,Python shutil.rmtree:无法删除win7上的git目录,python,git,windows-7,directory,Python,Git,Windows 7,Directory,windows 7 prof 64位上python 2.7的简单测试用例: 我通过python签出目录中的git项目,比如说,c:/temp/project。 之后,我用python命令删除它 shutil.rmtree('c:/temp/project') 执行该命令后,文件夹为空(无隐藏文件),但由于以下错误,无法将其删除: WindowsError: [Error 32] The process cannot access the file because it is being use
c:/temp/project
。
之后,我用python命令删除它
shutil.rmtree('c:/temp/project')
执行该命令后,文件夹为空(无隐藏文件),但由于以下错误,无法将其删除:
WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'C:\\temp\\project'
我已经检查过了,git当时没有运行(我甚至尝试过睡眠(10)来确保)。
我尝试过这个解决方案:
但它不起作用,同样的错误。
尝试了一个os.system('rmdir')
,但出现了相同的错误。
尝试了win32api.SetFileAttributes()
函数,但出现相同错误。
如果我通过资源管理器删除文件夹,就没有问题了
如何解决此问题?您可能正在试图删除的文件夹中执行Python代码。OP以错误的方向运行。。。但是我发现这个线程在使用GitPython时有问题;这似乎是一种常见的情况,因为如果您不以一些奇怪的方式清理,git python将为您的回购提供句柄:
import gc, stat
gc.collect()
your_repo_obj.git.clear_cache()
# now this will succeed:
shutil.rmtree(your_repo_dir)
对这些体操的需求既有缺陷,也有设计上的原因。此错误描述了原因:
您不是在C:\\temp\\project文件夹中执行此Python脚本,是吗?我是个白痴,谢谢:)这些事情都会发生。我添加了一个答案,请勾选!:)