[Tutor] subprocess figuring out what is in stdout

Nathan Smith nathan-tech at hotmail.com
Tue May 17 14:32:42 EDT 2022


Hi Mats,


thanks for that. I did end up going down pexpect 's path, though I went 
with winpexpect for windows.


Worked a treat.

Nathan


On 17/05/2022 15:34, Mats Wichmann wrote:
> 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://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpexpect.readthedocs.io%2Fen%2Fstable&data=05%7C01%7C%7C4da2f22d48ae40ee40f108da3812996b%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637883949776396684%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=W73FQxzF2dbAvQlozmavHMTAbIlqCluU0QuWXC%2FZIQA%3D&reserved=0) - 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://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpexpect.readthedocs.io%2Fen%2Fstable%2FFAQ.html%3Fhighlight%3Dunbuffer&data=05%7C01%7C%7C4da2f22d48ae40ee40f108da3812996b%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637883949776396684%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=Uo1LVcpOBi%2BJuNtugMuPCfJuGQfjHvVEyrNfYMKVf2k%3D&reserved=0
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://nam12.safelinks.protection.outlook.com/?url=https%3A%2F%2Fmail.python.org%2Fmailman%2Flistinfo%2Ftutor&data=05%7C01%7C%7C4da2f22d48ae40ee40f108da3812996b%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637883949776396684%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=OJc5sIX0ctguw5CeTBdXXwSuNYSldv3tNO24KyBhCoc%3D&reserved=0
-- 

Best Wishes,

Nathan Smith, BSC


My Website: https://nathantech.net




More information about the Tutor mailing list