[英]How do I delete a temp directory on Windows after using gitpython?
我有以下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中的臨時目錄?
在Windows中花費了大量 精力修復此行為,其中文件鎖定是文件系統的默認行為,但正如限制中所述:
[GitPython]是在析構函數(在
__del__
方法中實現)仍在確定性地運行的時間編寫的。“如果您仍想在這樣的上下文中使用它,您將需要在代碼庫中搜索
__del__
實現,並在您認為合適時自己調用它們。
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)
注意:這是
repo.close()
自2.1.1(2016年12月)以來的作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.