0

I am not able to override/delete the folder containing .git in python. I am working on the below configuration:

  • OS - Windows 8
  • Python - 3.5.2
  • Git - 2.9.2

Any Help would be appreciated.

BadHorsie
  • 14,135
  • 30
  • 117
  • 191
user3582164
  • 1
  • 2
  • 4
  • What is your intent with that? Please provide your code, that is not working. – Steffen Harbich Sep 01 '16 at 08:34
  • path='D:/code/taget/partnerrelationshipmanagement_V2/' def handleRemoveReadonly(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 shutil.rmtree(path, ignore_errors=True, onerror=handleRemoveReadonly) – user3582164 Sep 01 '16 at 11:39
  • Please [edit](http://stackoverflow.com/posts/39266408/edit) your question to add the code formatted. And please add the "python" tag to your question. – Steffen Harbich Sep 01 '16 at 13:57
  • Does this answer your question? [How to remove git repository, in python, on windows](https://stackoverflow.com/questions/58878089/how-to-remove-git-repository-in-python-on-windows) – tejasvi88 Aug 09 '21 at 02:05

3 Answers3

0

You should be using shutil to remove a directory that's not empty. If os.rmdir is used on a directory that's not empty an exception is raised.

import shutil

shutil.rmtree('/.git')
Sarhad Salam
  • 428
  • 3
  • 12
  • 1
    whenever to delete directory containing .git will raise PermissionError: [WinError 5] Access is denied: – user3582164 Sep 01 '16 at 09:55
  • Ok, this problem is raised in windows. Try seeing this StackOverflow Question: http://stackoverflow.com/questions/1213706/what-user-do-python-scripts-run-as-in-windows – Sarhad Salam Sep 01 '16 at 10:09
  • Above link also does not tell about to delete folder which is having .git – user3582164 Sep 01 '16 at 11:25
0

try this code

import shutil
shutil.rmtree('/path/to/your/dir/')
sr3z
  • 400
  • 1
  • 2
  • 10
  • 2
    it does not work out for folder which is having .git inside. – user3582164 Sep 01 '16 at 11:40
  • you should check permissions, it could be that you have no permission for .git directory and it has no relation to python. Either change permissions to .git directory or run script as root, in that case – sr3z Sep 01 '16 at 12:11
-1

try this, It will work

os.system('rmdir /S /Q "{}"'.format(baseDirectory)) 
Derrick
  • 3,669
  • 5
  • 35
  • 50