Python shutil.rmtree: cannot remove git dir on win7

Simple test case with python 2.7 on windows 7 prof 64 bits:
via python I checkout a git project in a directory, let’s say, c:/temp/project.
Afterwards I delete it with the python command

shutil.rmtree('c:/temp/project')

After the command, the folder is empty (no hidden files) but it cannot be removed it self because of the following error:

  • .gitignore to ignore all files, then recursively allows files of a certain type
  • Gitignore won't ignore .vs folder for Visual Studio 2015 RC on Windows7/8
  • How to set a folder to be used in different branches in git?
  • Does the folder I am in have any significance in git?
  • SVN to Git. New branch with new source
  • Git - How to remove one of two folders with the same name in remote repo
  • WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'C:\\temp\\project'
    

    I’ve checked and git is not running at that moment (I’ve even tried a sleep(10) to be sure).
    I’ve tried this solution:

    What user do python scripts run as in windows?

    but it doesn’t work, same error.
    Tried a os.system('rmdir') but same error.
    Tried win32api.SetFileAttributes() function but same error.
    If I delete the folder via explorer, there’s no problem.

    How can I solve the problem?

  • Unknown option git config --local reported by Jenkins
  • Application rebrands - when should a branching strategy be employed?
  • Accessing the git shell in GitX on OS X
  • Visual Studio Code cannot detect cygwin git.exe path
  • clearcase snapshot hijacked files : how to checkout/checkin changed files
  • Git pull --rebase converting spaces to tabs
  • 2 Solutions collect form web for “Python shutil.rmtree: cannot remove git dir on win7”

    You are probably executing the Python code inside the folder you are trying to remove.

    The OP was running in the wrong dir … but i found this thread for a problem using GitPython; seems like a common case, as git-python will hold handles to your repo if you don’t clean up in some odd ways:

    import gc, stat
    
    gc.collect()
    your_repo_obj.git.clear_cache()
    
    # now this will succeed: 
    shutil.rmtree(your_repo_dir)
    

    the need for these gymnastics are due to both a bug, and by design. This bug describes the reasons:
    https://github.com/gitpython-developers/GitPython/issues/553

    Git Baby is a git and github fan, let's start git clone.