gpt4 book ai didi

python - 使用 gitpython 后如何删除 Windows 上的临时目录?

转载 作者:可可西里 更新时间:2023-11-01 09:28:59 24 4
gpt4 key购买 nike

我有以下 Python 函数,我在 Windows 7 上运行:

def update():
temp_dir = tempfile.mkdtemp()
git.Git().clone('my_repo', temp_dir)
try:
repo = git.Repo(temp_dir)
repo.index.add('*')
repo.index.commit('Empty commit')
finally:
from git.util import rmtree
rmtree(temp_dir)

不幸的是,在 rmtree 行上,我得到:

WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'c:\\users\\myaccount\\appdata\\local\\temp\\tmpdega8h\\.git\\objects\\pack\\pack-0ea07d13498ab92388dc33fbabaadf37511623c1.idx'

我应该如何删除 Windows 中的临时目录?

最佳答案

A loteffort已花费在 Windows 中修复此行为,其中文件锁定是文件系统的默认行为,但如 Limitations 中所述:

[GitPython] was written in a time where destructors (as implemented in the __del__ method) still ran deterministically."

In case you still want to use it in such a context, you will want to search the codebase for __del__ implementations and call these yourself when you see fit.

无论如何,this the trick由项目的测试用例使用可能有助于:

repo_dir = tempfile.mktemp()
repo = Repo.init(repo_dir)
try:
## use the repo

finally:
repo.git.clear_cache() # kill deamon-procs responding to hashes with objects
repo = None
if repo_dir is not None:
gc.collect()
gitdb.util.mman.collect() # kept open over git-object files
gc.collect()
rmtree(repo_dir)

NOTE: this is what repo.close() does since 2.1.1 (Dec 2016).

关于python - 使用 gitpython 后如何删除 Windows 上的临时目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23566538/

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