Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 638
Nick Avatar asked Sep 19 '16 06:09

Nick


People also ask

How do I delete a repository in Python?

In Python you can use os. rmdir() and pathlib. Path. rmdir() to delete an empty directory and shutil.

What does deleting .Git folder do?

The folder is created when the project is initialized (using git init ) or cloned (using git clone ). It is located at the root of the Git project repository. If we delete the . git folder, the information saved by Git will be lost, and the directory will no longer act as a Git repository.

What happens if I delete my Git file?

Deleting the . git folder does not delete the other files in that folder which is part of the git repository. However, the folder will no longer be under versioning control.


2 Answers

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.

like image 64
saurabh baid Avatar answered Sep 29 '22 06:09

saurabh baid


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:
like image 21
Vadym Shcherbakov Avatar answered Sep 29 '22 05:09

Vadym Shcherbakov