As the title describes, I need to remove a git repository by using python. I've seen other questions about this very same topic, but none of the solutions seem to work for me.
My work: I need to download a repository, using gitpython, followed by checking some irrelevant things. After the process is finished, I need to delete the repository to save space for whoever is using my script.
The problem: When cloning a git repository, a .git file will be created. This file is hidden in windows, and the modules I've been using do not have permission to delete any of the files within the .git folder.
What I've tried:
import shutil
shutil.rmtree('./cloned_repo')
PermissionError: [WinError 5] Access is denied:
Any help regarding this issue would be deeply appreciated.
Git has some readonly files. You need to change the permissions first:
import subprocess
import shutil
import os
import stat
from os import path
for root, dirs, files in os.walk("./cloned_repo"):
for dir in dirs:
os.chmod(path.join(root, dir), stat.S_IRWXU)
for file in files:
os.chmod(path.join(root, file), stat.S_IRWXU)
shutil.rmtree('./cloned_repo')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With