【问题标题】:Python shutil.rmtree: cannot remove git dir on win7Python shutil.rmtree:无法在 win7 上删除 git 目录
【发布时间】:2014-03-13 18:07:03
【问题描述】:

在 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 used by another process: 'C:\\temp\\project'

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

What user do python scripts run as in windows?

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

我该如何解决这个问题?

【问题讨论】:

  • 您不是从 C:\\temp\\project 文件夹中执行此 Python 脚本,对吗?
  • 我是个白痴,谢谢:)
  • 这些事情发生了。我已经添加了答案,请打勾! :)

标签: python git windows-7 directory


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您可能正在尝试删除的文件夹中执行 Python 代码。

    【讨论】:

      猜你喜欢
      • 2021-02-27
      • 1970-01-01
      • 1970-01-01
      • 2010-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-09
      相关资源
      最近更新 更多