gpt4 book ai didi

Python 子进程/Popen stdout 被截断

转载 作者:太空宇宙 更新时间:2023-11-04 05:56:49 35 4
gpt4 key购买 nike

Popen 标准输出的输出与从 Shell 或使用 os.system 运行命令不同:

cmd = ['git', 'diff', commitHash, '--stat']

print Popen(cmd, stdout=PIPE).stdout.read().strip()
os.system(' '.join(cmd))

Popen 的输出:

 src/tech/dalvik/sidebar.md                         |   10 -
.../encryption/android_crypto_implementation.md | 359 --
src/tech/encryption/index.md | 22 -
src/tech/encryption/sidebar.md | 9 -
src/tech/index.md | 58 -
src/tech/nfc/index.md | 25 -
src/tech/nfc/sidebar.md | 7 -
src/tech/sidebar.md | 5 -
templates/footer | 3 +-
templates/includes | 2 +-
templates/sidebar | 2 +-
257 files changed, 32311 insertions(+), 11358 deletions(-)

os.system 的输出(与从 Shell 运行相同)

 src/tech/dalvik/sidebar.md                                         |   10 -
src/tech/encryption/android_crypto_implementation.md | 359 ---------
src/tech/encryption/index.md | 22 -
src/tech/encryption/sidebar.md | 9 -
src/tech/index.md | 58 --
src/tech/nfc/index.md | 25 -
src/tech/nfc/sidebar.md | 7 -
src/tech/sidebar.md | 5 -
templates/footer | 3 +-
templates/includes | 2 +-
templates/sidebar | 2 +-
257 files changed, 32311 insertions(+), 11358 deletions(-)

如何让 subprocess.Popenos.system 一样工作?

我在这里发现了一个类似的问题:Stdout captured from pipe in Python is truncated , 但设置 LANG 环境变量似乎不起作用。

谢谢!

最佳答案

我不认为输出实际上被截断了。

可能 git 给你不同的输出,试图最好地向你展示差异统计。

根据您的示例,我猜问题出在环境变量 COLUMNS 的不同值上。

检查您的终端有多少列:

$ echo $COLUMNS

并在 Popen 调用中设置此环境变量:

Popen(cmd, stdout=PIPE, env={'COLUMNS':'249'})

关于Python 子进程/Popen stdout 被截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27328428/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com