我想编写一个简单的 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
了解详情。
