Python脚本未在Windows中删除Git文件

Nic*_*ick 5 python windows git delete-file

我正在使用以下代码删除包含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
Run Code Online (Sandbox Code Playgroud)

此代码应能很好地处理只读文件。我可以从Windows资源管理器中删除目录/文件夹,但是当我运行以下代码时:

if __name__ == '__main__':
    clear_dir(r'c:\path\to\ci-monitor')
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

  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'                                                                                
Run Code Online (Sandbox Code Playgroud)

我尝试以管理员身份运行脚本(无更改。)

被删除的目录是一个git repo,我会定期克隆,检查和删除它。检查是为了确保存储库中没有未合并的发行版和修补程序分支。

任何人有任何想法吗?

sau*_*aid 2

如果该文件正在被另一个进程使用,则无法删除它。使用“解锁器”或任何其他类似软件进行交叉检查。

  • 感谢您的回答,但如果是这样的话,我将无法在 Windows 资源管理器中删除它 (2认同)
  • 另一种可能性是文件是用“FILE_SHARE_DELETE”打开的,然后在没有关闭句柄的情况下被删除。在关闭所有引用之前,Windows 不会取消文件与目录的链接。如果文件当前被“删除”(这只是文件控制块上的一个标志),则无法为任何访问打开新句柄。删除文件需要使用“DELETE”访问权限打开句柄,因此“DeleteFile”将失败并显示“ERROR_ACCESS_DENIED”。在文件取消链接之前也无法删除父目录。 (2认同)