Search code examples
pythonlinuxstdoutstderrpexpect

Is it possible to pre-pend each STDERR with a given string


I am writing a program to interact with a linux machine through the serial port, and I am using pexpect.spawn as my main communication channel as follows:

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

and I am sending commands to the machine with the sendline("cmd") method, and at the end of each session I parse the log file to see how the commands behaved.

I would like to be able to distinguish between lines that were printed to stdout and stderr from my log file, but currently I have no way of doing that. Is that a way to globally prepend each line printed to stderr with a given string?


Solution

  • You don't mention how you capture stdout and stderr, but one simple way distinguish the stdout and stderr is to simply place stdout and stderr in different files. For example:

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