Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove git repository, in python, on windows

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.

like image 206
Kuratorn Avatar asked Nov 15 '19 13:11

Kuratorn


1 Answers

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')
like image 136
Anonymous Avatar answered Oct 02 '22 06:10

Anonymous