python - 是否可以在每个 STDERR 前面加上给定的字符串

标签 python linux stdout stderr pexpect

我正在编写一个程序通过串口与Linux机器交互,我使用pexpect.spawn作为我的主要通信 channel ,如下所示:

proc = pexpect.spawn("cu dir -l /dev/ttyUSB0 -s 115200", logfile = *someFile*)

我使用 sendline("cmd") 方法向计算机发送命令,并在每个 session 结束时解析日志文件以查看命令的行为方式。

我希望能够区分从日志文件打印到 stdout 和 stderr 的行,但目前我无法做到这一点。 这是一种在打印到 stderr 的每一行之前全局添加给定字符串的方法吗?

最佳答案

您没有提到如何捕获 stdout 和 stderr,但区分 stdout 和 stderr 的一种简单方法是将 stdout 和 stderr 放在不同的文件中。例如:

./command.py >stdout-log 2>stderr-log

关于python - 是否可以在每个 STDERR 前面加上给定的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9524934/

相关文章:

python - 使用 Python RSA 将请求复制到 Chef

python - 为 MNIST OCR 预处理图像

linux - shell脚本中 "&&"内部变量和外部变量的区别

python - Python 中的 "stderr"有什么意义?

Python - 具有相同名称但内容不同的文件

python - 命令 '/bin/sh -c pip install -r requirements.txt' 返回非零代码 : 1

html - 找不到我正在使用的 index.html

linux - 当/var 文件系统使用率达到阈值时发出警报的程序

linux - bash linux - 从标准输入和标准输出写入和读取

asynchronous - 从标准输出中删除流以便添加另一个流的正确方法是什么?