首页 > 解决方案 > 从 python 调用时如何捕获 git bash 的命令行打印输出?

问题描述

假设我有 3 个文本文件 ours.txt、base.txt 和 theirs.txt,并且想要对它们进行三向合并。当我调用git merge-file -p ours.txt base.txt theirs.txtGit Bash 时,它会打印合并的文本。

然而,当我跑步时

import subprocess

dir = "path/to/text files"
cmd = ["git", "merge-file", "-p ", "ours.txt", "base.txt", "theirs.txt"]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, cwd=dir)

我可以访问stdout stderr通过

(out, error) = p.communicate()

但似乎无法将在 Git Bash 中打印的合并文本存储在变量中。

有人对如何检索它有任何想法吗?

提前致谢。

标签: pythonbashgitsubprocessmerge-file

解决方案


推荐阅读