【发布时间】:2020-03-20 04:43:28
【问题描述】:
我有一个用 Python 实现的自定义 Git 命令,它使用来自 subprocess 模块的方法来调用 git。我注意到,根据使用的方法,输出可能会或可能不会被着色,例如:
import subprocess
subprocess.run('git push origin --delete foobar', shell=True)
print()
print(subprocess.run('git push origin --delete foobar', shell=True,
capture_output=True, encoding='utf-8').stderr, end='')
输出:
如何在捕获的输出(stdout 和 stderr)中保留颜色?
【问题讨论】:
-
这不能回答您的问题,但请考虑使用the Python bindings for libgit2 而不是
subprocess。它应该提供比构建字符串更好的界面以及更好的性能和跨平台支持,而无需用户的$PATH上的git。
标签: python git terminal subprocess