dos promt simulation

Bengt Richter bokr at oz.net
Sun Apr 14 07:26:48 EDT 2002


On 14 Apr 2002 01:19:23 -0700, theivo at abv.bg (I.C.) wrote:

>bokr at oz.net (Bengt Richter) wrote in message news:<a9aqr4$moh$0 at 216.39.172.122>...
>> On 13 Apr 2002 14:39:58 -0700, theivo at abv.bg (I.C.) wrote:
>> 
>> >> there is also popen2, popen3 etc. they give you a file for stdin where you 
>> >> can write to.
>> >
>> >Maybe popen2\3 could help, but I think, I don't use them correctly :
>> >
>> >import os
>> >dos_input, dos_output = os.popen2('time')
>> >
>> >for line in dos_output.readlines(): # Current time is 22:14:43,24
>> >        print line                  # Enter new time:
>> >
>> >dos_input.write(raw_input())        # User enters the new time
>>   ^^^^^--??
>> >
>> >Where is my mistake?
>> >
>> I would guess that's probably one.
>> 
Sorry, that _is_ the child process stdin. I wasn't paying very good attention ;-/
After my comment, I felt obligated to try a few things, but could not get a volley going
between the interactive python and a python script executed via popen2('python rwrw.py').
I did find that writing all that the other script wanted as input into the pipe first made
it do its thing and produce everything back in one gob.

I tried writing to stdout and flushing in the other script, and still couldn't read anything
before supplying all its input, even though it suposedly wrote first. I tried to control the
buffer size to 1, but that didn't do anything either. It looks almost like you have to close
the pipe at the child end to have the output available to popen. Maybe sockets is a better idea
for communicating back and forth with another process. Mostly just have to decide on a message
delimiter, presumably.

I remember a large hassle getting pipes to work in C on windows, so it would not surprise me
to find limitations the way you can use them in windows popen2. It ought to be possible to do
something quite flexible under NT though. Just a lot of work to find the squirrely bits
of info you need. It wouldn't surprise me if some windows versions just fake pipes via hidden
temp files that have to be closed before they're opened by the recipient. At least it's acting
like that. But NT wouldn't have to, so I don't know what's happening. And I can't chase it now.

Regards,
Bengt Richter



More information about the Python-list mailing list