如何在 Windows 上的 python 中删除 git 存储库 [英] How to remove git repository, in python, on windows

查看:39
本文介绍了如何在 Windows 上的 python 中删除 git 存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所描述的,我需要使用 python 删除一个 git 存储库.我已经看到了关于同一主题的其他问题,但似乎没有一个解决方案适合我.

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.

我的工作:我需要使用 gitpython 下载一个存储库,然后检查一些不相关的东西.该过程完成后,我需要删除存储库,以便为使用我的脚本的人节省空间.

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.

问题:克隆 git 存储库时,将创建一个 .git 文件.这个文件隐藏在windows中,我一直在使用的模块没有权限删除.git文件夹中的任何文件.

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.

我尝试过的:

import shutil
shutil.rmtree('./cloned_repo')

PermissionError: [WinError 5] Access is denied:

对于此问题的任何帮助将不胜感激.

Any help regarding this issue would be deeply appreciated.

推荐答案

Git 有一些只读文件.您需要先更改权限:

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')

这篇关于如何在 Windows 上的 python 中删除 git 存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆