python - 使用命令提示符下的 python 脚本从 git bash 克隆 git repo 失败。为什么?
问题描述
git bash 突然关闭而不克隆 repo。我无法理解这里有什么问题。
import os
import subprocess
parent_dir = r'C:\Users\user\Documents'
dir_name = 'Git_temp'
dir_path = os.path.join(parent_dir, dir_name)
os.mkdir(dir_path)
print("{0} created under {1}".format(dir_name, parent_dir))
os.chdir(dir_path)
print("Current Working Directory : {0}".format(os.getcwd()))
git_file = "C:\Program Files\Git\git-bash.exe"
git_clone_cmd = "git clone https://github.com/patankar-saransh/test_repo.git"
subprocess.Popen([git_file, git_clone_cmd])
解决方案
如果您不想使用GitPython,那么只需确保git.exe
在您的%PATH%
.
那么您的电话将是:
import subprocess
process = subprocess.Popen(["git", "clone", "https://..."], stdout=subprocess.PIPE)
output = process.communicate()[0]
如此处所示,对于 Python 2.7+,使用check_output
import subprocess
output = subprocess.check_output(["git", "clone", "https://..."]])
推荐阅读
- vba - 如何使用 VBA 从 Outlook 电子邮件中提取“类型” - 电子邮件或 Web?
- javascript - Next Js 为 Strapi 中创建的页面数据动态更改模板布局
- blazor - Blazor 输入:onblur 抛出,oninput 工作正常
- javascript - 如何显示绝对项目但能够在其下方单击?
- android - 如何从 Android 的 Firebase 数据库制作倒数计时器
- javascript - 如何从 GitHub API 获取所有信息
- sql - 如何在 SQL 中用以前的值填充 NULL
- javascript - 角度形式值 {} 在 ts 文件中为空,但在 html 中有效
- php - 如何设置 github 操作 Laravel
- c# - 现有对象的字符串