gpt4 book ai didi

python - 从 Python 程序响应 git 命令行提示符

转载 作者:太空宇宙 更新时间:2023-11-03 16:54:14 25 4
gpt4 key购买 nike

我目前正在使用 Python 程序在对文件进行一些编辑后自动提交到存储库。我正在使用 subprocess.Popen 来创建一个调用 git 二进制文件的子进程。我虽然在创建子进程时可以使用 stdin=PIPE ,并且能够在命令行提示时写入我的用户凭据,但由于某种原因,这种情况没有发生。代码如下所示:

proc = Popen(["git","push","origin","master"], stdin=PIPE)
proc.communicate("username")
proc.communicate("password")

现在发生的事情是它正在调用二进制文件,但仍然向我显示命令行提示符。我在这里查看了其他一些问题,但它们基本上都使用与我相同的代码,所以我对这个问题有点困惑。我知道还有很多其他方法可以实现此结果,例如缓存我的凭据,但我很好奇这种方法是否可行,原因有很多。

最佳答案

proc = Popen(["git","push","origin","master"], stdin=PIPE,stdout=PIPE,stderr=PIPE)
time.sleep(1)
proc.stdin.write("username")
time.sleep(1)
proc.stdin.write("password")
#or maybe
for letter in "password":
proc.stdin.write(letter)
time.sleep(0.1)
print proc.communicate()

我猜是这样的......

关于python - 从 Python 程序响应 git 命令行提示符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35589206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com