gpt4 book ai didi

python - 在 python 中捕获致命的 bash/git

转载 作者:太空宇宙 更新时间:2023-11-04 07:36:32 25 4
gpt4 key购买 nike

我想编写一个简单的 python 脚本,它将能够将 git 存储库克隆到所需的目录中。我使用 try...except 构造来捕获所有异常,但看起来我无法正确处理“致命”。

#!/usr/bin/env python
import subprocess

try:
subprocess.check_call(['git', 'clone', 'git clone git@some_repo', '/tmp/some_directory'])
except Exception:
print "There was a problem during repository configuration"

上面脚本的输出:

fatal: repository 'git clone git@some_repo' does not exist

There was a problem during repository configuration

更具体地说,我更希望收到“有一个...”消息。为什么我也会收到“致命”消息?

最佳答案

您需要捕获 subprocess.check_call() 执行的 STDERR。参见 Catch stderr in subprocess.check_call without using subprocess.PIPE

了解详情。

关于python - 在 python 中捕获致命的 bash/git,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33902590/

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