【问题标题】:Python subprocess/Popen stdout is truncatedPython 子进程/Popen 标准输出被截断
【发布时间】:2023-04-06 21:15:02
【问题描述】:

Popen stdout 的输出与从 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 环境变量似乎不起作用。

谢谢!

【问题讨论】:

    标签: python pipe subprocess popen


    【解决方案1】:

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

    可能git 正在为您提供不同的输出,试图最好地向您展示差异统计数据。

    从您的示例中,我猜问题是 envvar COLUMNS 的值不同。

    检查您的终端有多少列:

    $ echo $COLUMNS

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

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

    【讨论】:

      猜你喜欢
      • 2016-07-09
      • 1970-01-01
      • 2014-08-28
      • 1970-01-01
      • 2020-07-05
      • 2011-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多