如何在 Windows 上的 python 中删除 git 存储库

Kur*_*orn 4 python windows git gitpython

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

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

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

我试过的:

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

PermissionError: [WinError 5] Access is denied:
Run Code Online (Sandbox Code Playgroud)

任何有关此问题的帮助将不胜感激。

Ano*_*ous 5

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

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')
Run Code Online (Sandbox Code Playgroud)