【问题标题】:Get output colored text with Popen使用 Popen 获取输出彩色文本
【发布时间】:2016-04-10 01:10:38
【问题描述】:

我正在使用 popen 制作插件,该插件使用了一个外部程序,该程序在输出中显示了一些彩色文本。

输出是这样的:

avr-g++ -o .pioenvs\uno\FrameworkArduino\HardwareSerial.o -c -std=gnu++11 -fno-exceptions -fno-threadsafe-statics -g -Os -Wall -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO_AVR_UNO -DARDUINO=10607 -I.pioenvs\uno\FrameworkArduino -I.pioenvs\uno\FrameworkArduinoVariant .pioenvs\uno\FrameworkArduino\HardwareSerial.cpp    
avr-g++ -o .pioenvs\uno\FrameworkArduino\HardwareSerial0.o -c -std=gnu++11 -fno-exceptions -fno-threadsafe-statics -g -Os -Wall -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO_AVR_UNO -DARDUINO=10607 -I.pioenvs\uno\FrameworkArduino -I.pioenvs\uno\FrameworkArduinoVariant .pioenvs\uno\FrameworkArduino\HardwareSerial0.cpp
=============================================
Path\file.cpp: in function 'void loop()':
Path\file.cpp:23:2 error: expected ';' before '}' token
}
^
=============================================

“=”内的所有内容都是红色和黄色的。

当我在命令控制台中运行命令时,我可以看到完整的输出,但是当我使用 Popen 时,我只能得到未着色的文本

这就是我使用 Popen 的方式

process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, cwd=self.cwd, universal_newlines=True, shell=True)
output = process.communicate()

stdout = output[0]
stderr = output[1]

print(stdout)
print(stderr)

我想获取文本,即使它没有着色,重要的是获取完整的日志。

任何建议将不胜感激

【问题讨论】:

  • 如果我的答案有助于解决您的问题,请投票。 :)
  • 完成!再次感谢:)

标签: python python-2.7 output popen


【解决方案1】:

您无法获得所有消息,因为您的命令输出的一部分不是常规输出,它被视为错误(或日志或调试消息)

现在您可以将 stderr=subprocess.PIPE 添加到 Popen 的参数中,这会将所有错误放入您的 stderr 变量中:

process = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=self.cwd, universal_newlines=True, shell=True)
output = process.communicate()

stdout = output[0]
stderr = output[1]

print(stdout)
print(stderr)

或者,如果您希望所有错误和输出与您在控制台中看到的一样,请在命令末尾添加2>&1。类似:

avr-g++ -o .pioenvs\uno\FrameworkArduino\HardwareSerial.o -c -std=gnu++11 -fno-exceptions -fno-threadsafe-statics -g -Os -Wall -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -DARDUINO_ARCH_AVR -DARDUINO_AVR_UNO -DARDUINO=10607 -I.pioenvs\uno\FrameworkArduino -I.pioenvs\uno\FrameworkArduinoVariant .pioenvs\uno\FrameworkArduino\HardwareSerial.cpp 2>&1

【讨论】:

  • 谢谢@Rahman,最后一个建议解决了我的问题。您是否有阅读有关该解决方案的链接,或者您能解释一下2>&1 的含义吗?
  • @GEPD 这是我在回复您之前阅读的链接stackoverflow.com/a/818284/811922
  • @GEPD:要了解如何保留颜色代码,请查看avr-g++ man page 中的-fdiagnostics-color[=WHEN]
  • 谢谢@PM2Ring 我现在看看,我没有直接使用 avr-g++,但很高兴知道这些额外的信息。
【解决方案2】:

当我在命令控制台中运行命令时,我可以看到完整的输出,但是当我使用 Popen 时,我只能得到未着色的文本

可能有两个问题:

  1. 彩色文本恰好是在 stderr 上生成的,而不是在 stdout 上生成的。要捕获标准错误,请设置stderr=PIPEas @Rahman suggested

  2. avr-g++ 命令如果检测到输出不是 tty 可能会禁用颜色,例如,如果它被重定向到与您的情况类似的管道。要启用颜色,请传递命令行选项 -fdiagnostics-color=always as @PM 2Ring suggested

    或者(不太便携,但它可能与更多命令一起使用)提供一个伪 tty 来欺骗avr-g++ 命令,使其认为它以交互方式运行(因此它应该启用颜色)。你可以使用pty.spawn(), pexpect module

    import pexpect # $ pip install pexpect
    
    output, exitstatus = pexpect.runu(command, withexitstatus=1)
    

    或(更底层)使用pty.openpty() + subprocess module

【讨论】:

    猜你喜欢
    • 2013-09-21
    • 2019-05-06
    • 2021-09-28
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 2013-05-26
    • 2012-06-26
    • 1970-01-01
    相关资源
    最近更新 更多