python - 从 python 调用时如何捕获 git bash 的命令行打印输出?
问题描述
假设我有 3 个文本文件 ours.txt、base.txt 和 theirs.txt,并且想要对它们进行三向合并。当我调用git merge-file -p ours.txt base.txt theirs.txt
Git 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 中打印的合并文本存储在变量中。
有人对如何检索它有任何想法吗?
提前致谢。
解决方案
推荐阅读
- python - Python / Flask App - 相对导入停止工作
- unix-text-processing - 从文件中删除括号
- java - Spring MVC 中的通知系统
- algorithm - 合并排序算法中 (log2 N +1) x 6N 中的“+1”是什么?
- html - CSS Grid - 在三列中定位文本
- r - 如何最好地将季度数据转换为月度数据
- python - 如果可能,如何使用 os 或 subprocess 一个接一个地执行命令行程序中的命令?
- php - 此收款实例支付刀片上不存在属性 [id]
- reactjs - React:如何制作快速的可折叠元素?
- python - Python:中断终端铃声/系统铃声以模拟莫尔斯电码 DOT/DASH 消息