[Tutor] subprocess figuring out what is in stdout

Mats Wichmann mats at wichmann.us
Tue May 17 10:34:26 EDT 2022


On 5/16/22 20:05, Nathan Smith wrote:
> I'm working with a program which requires interactive shell like reading
> of the output, EG user types input in response to various outputs
> displayed to them.
> 
> Unfortunately, in some cases, the program being run is not either
> printing an "\n" at the end of particular lines, or is not flushing  it.

It's not entirely clear what you're trying to accomplish, so this is
guessing...

Getting this to work well depends on how much control you have over the
external program. If you don't have control over it, you might look at
how the Python version of expect works
(https://pexpect.readthedocs.io/en/stable) - or maybe even be able to
use it?

It's normally up to the external process to decide how to handle its own
stdout/stderr. You can sometimes make things work by wiring up the
plumbing appropriately.  On Linux, the standard library does buffering
if the output is a pipe, so if you can "make it look like a terminal"
(i.e. plug in a pseudo-tty), it might work better for you - see
pexpect's FAQ on this topic:

https://pexpect.readthedocs.io/en/stable/FAQ.html?highlight=unbuffer



More information about the Tutor mailing list