从python调用时,如何捕获git bash的命令行打印输出?

2024-04-23 18:50:25 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有3个文本文件ours.txt、base.txt和theres.txt,并希望对它们进行三向合并。当我在GitBash中调用git merge-file -p ours.txt base.txt theirs.txt时,它将打印合并的文本

然而,当我跑步的时候

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)

我可以通过

(out, error) = p.communicate()

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

有人知道如何取回它吗

提前谢谢


Tags: 文本gittxtcmdbasedirmerge跑步