gpt4 book ai didi

Python shutil.rmtree : cannot remove git dir on win7

转载 作者:太空狗 更新时间:2023-10-29 13:47:13 26 4
gpt4 key购买 nike

在 Windows 7 prof 64 位上使用 python 2.7 的简单测试用例:通过 python,我在一个目录中 checkout 一个 git 项目,比方说,c:/temp/project。然后我用python命令删除它

shutil.rmtree('c:/temp/project')

执行命令后,文件夹为空(无 stash 文件)但由于以下错误无法自行删除:

WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'C:\\temp\\project'

我已经检查过,此时 git 没有运行(我什至尝试过 sleep(10) 来确定)。我试过这个解决方案:

What user do python scripts run as in windows?

但它不起作用,同样的错误。尝试了 os.system('rmdir') 但同样的错误。尝试了 win32api.SetFileAttributes() 函数但同样的错误。如果我通过资源管理器删除该文件夹,则没有问题。

我该如何解决这个问题?

最佳答案

OP 在错误的目录中运行...但我发现此线程使用 GitPython 时出现问题;似乎是一个常见的情况,因为如果你不以一些奇怪的方式清理,git-python 将持有你的 repo 的句柄:

import gc, stat

gc.collect()
your_repo_obj.git.clear_cache()

# now this will succeed:
shutil.rmtree(your_repo_dir)

对这些体操的需要是由于错误和设计造成的。这个错误描述了原因: https://github.com/gitpython-developers/GitPython/issues/553

关于Python shutil.rmtree : cannot remove git dir on win7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21778356/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com