【问题标题】:Print stdout subprocess Popen and retain formatting打印 stdout 子进程 Popen 并保留格式
【发布时间】:2017-09-25 14:43:44
【问题描述】:

我正在使用subprocess.Popen 调用安装脚本。但是,当我通过print 将输出传送到终端时,它会丢失安装脚本输出中的任何格式。例如,所有终端颜色都消失了,下载进度条出现在多行。

这是我的代码:

process = subprocess.Popen(
    [script],
    shell=True,
    cwd=script_dir,
    universal_newlines=True,
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT,
    env={
        'WORKSPACE': build_dir
    })
with process.stdout:
    for line in iter(process.stdout.readline, b''):
        print(line)

还有一个输出样本(通常显示为进度条):

You need to run "nvm install 4.2.2" to install it before using it.
Downloading and installing node v4.2.2...
Downloading https://nodejs.org/dist/v4.2.2/node-v4.2.2-darwin-x64.tar.gz...

                                                                          0.0%
                                                                          1.0%
###                                                                        4.6%
#######                                                                   10.5%
#############                                                             18.8%
###################                                                       26.5%
########################                                                  34.1%
##############################                                            41.9%
###################################                                       49.7%
#########################################                                 57.4%
##############################################                            65.2%
####################################################                      73.0%
##########################################################                80.8%
###############################################################           88.5%
#####################################################################     96.3%
######################################################################## 100.0%

【问题讨论】:

    标签: python python-2.7 subprocess


    【解决方案1】:

    print statement 在末尾打印额外的换行符。请改用sys.stdout.write

    import sys
    
    ...    
    
    with process.stdout:
        for line in iter(process.stdout.readline, b''):
            sys.stdout.write(line)  # <--
            sys.stdout.flush()      # <--
    

    顺便说一句,你可以只使用subprocess.call 而不捕获stdout、stderr 输出,除非你想在你的程序中处理输出:

    subprocess.call(
        [script],
        shell=True,
        cwd=script_dir,
        env={
            'WORKSPACE': build_dir
        })
    

    【讨论】:

    • 你说得对,sys.stdout.write 没有添加换行符,但是它仍然使进度条的每一步都出现在自己的行上。似乎终端 ANSI 转义码在某个时候被删除了。
    • 经过一番挖掘,看起来在使用subprocess.PIPE 时根本没有保留 ANSI 转义码。不幸的是,我确实需要处理一些输出,所以我不能只使用subprocess.call
    【解决方案2】:

    无需调用“iter”。事实上,当我尝试 this suggestion 时它会抱怨,因为它需要一个字符串,而不是字节。

    这可以重定向完全相同的输出:

    process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, universal_newlines=True)
    for line in process.stdout:
        sys.stdout.write(line)
        sys.stdout.flush()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-13
      • 1970-01-01
      • 1970-01-01
      • 2019-08-15
      • 1970-01-01
      • 2015-03-22
      • 1970-01-01
      • 2015-05-02
      相关资源
      最近更新 更多