Kim
Kim

Reputation: 1684

Python script not deleting Git files in Windows

I'm using the following code to delete a directory containing a 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

This code should deal well with read-only files. I can delete the directory/folder from Windows Explorer, but when I run the following code:

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

I get the following error:

  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'                                                                                

I've tried running the script as administrator (no change.)

The directory being deleted is a git repo, and I am periodically cloning, checking and deleting it. The checks are to make sure there are no unmerged release and hotfix branches in the repo.

Anyone got any ideas?

Upvotes: 7

Views: 4457

Answers (2)

Vadym Shcherbakov
Vadym Shcherbakov

Reputation: 21

I faced with the same issue. I was able to resolve it by adding os.unlink to the list of funcs:

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

Upvotes: 2

saurabh baid
saurabh baid

Reputation: 1877

If that file is being used by another process then it would not be possible to delete it. cross check it by using 'unlocker' OR any other similar software.

Upvotes: 2

Related Questions