簡體   English   中英

Python 腳本不刪除 Windows 中的 Git 文件

[英]Python script not deleting Git files in Windows

我正在使用以下代碼刪除包含 git repo 的目錄:

import errno
import os
import stat
import shutil


def clear_dir(path):
    shutil.rmtree(path, ignore_errors=False, onerror=handle_remove_readonly)


def handle_remove_readonly(func, path, exc):
  excvalue = exc[1]
  if func in (os.rmdir, os.remove) and excvalue.errno == errno.EACCES:
      os.chmod(path, stat.S_IRWXU| stat.S_IRWXG| stat.S_IRWXO) # 0777
      func(path)
  else:
      raise

這段代碼應該很好地處理只讀文件。 我可以從 Windows 資源管理器中刪除目錄/文件夾,但是當我運行以下代碼時:

if __name__ == '__main__':
    clear_dir(r'c:\path\to\ci-monitor')

我收到以下錯誤:

  File "C:\Users\m45914\code\ci-monitor\utils\filehandling.py", line 8, in clear_dir                              
    shutil.rmtree(path, ignore_errors=False, onerror=handle_remove_readonly)                                      
  File "C:\Users\m45914\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 488, in rmtree                
    return _rmtree_unsafe(path, onerror)                                                                          
  File "C:\Users\m45914\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 378, in _rmtree_unsafe        
    _rmtree_unsafe(fullname, onerror)                                                                             
  File "C:\Users\m45914\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 378, in _rmtree_unsafe        
    _rmtree_unsafe(fullname, onerror)                                                                             
  File "C:\Users\m45914\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 378, in _rmtree_unsafe        
    _rmtree_unsafe(fullname, onerror)                                                                             
  File "C:\Users\m45914\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 378, in _rmtree_unsafe        
    _rmtree_unsafe(fullname, onerror)                                                                             
  File "C:\Users\m45914\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 383, in _rmtree_unsafe        
    onerror(os.unlink, fullname, sys.exc_info())                                                                  
  File "C:\Users\m45914\AppData\Local\Programs\Python\Python35\lib\shutil.py", line 381, in _rmtree_unsafe        
    os.unlink(fullname)                                                                                           
PermissionError: [WinError 5] Access is denied: 'scratch\\repos\\ci-monitor\\.git\\objects\\pack\\pack-83e55c6964d
21e8be0afb2cbccd887eae3e32bf4.idx'                                                                                

我試過以管理員身份運行腳本(沒有變化。)

被刪除的目錄是一個 git repo,我會定期克隆、檢查和刪除它。 檢查是為了確保 repo 中沒有未合並的版本和修補程序分支。

有人有任何想法嗎?

如果該文件正被另一個進程使用,則無法刪除它。 使用“解鎖器”或任何其他類似軟件進行交叉檢查。

我遇到了同樣的問題。 我能夠通過將 os.unlink 添加到 funcs 列表來解決它:

if func in (os.rmdir, os.remove, os.unlink) and excvalue.errno == errno.EACCES:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM