[Tutor] Writing a script to interact with an interactive commandprogram

Demonic Software demonic.software at gmail.com
Tue Jun 10 02:55:41 CEST 2008


I have been working with the Popen class, and it hangs when I perform the
read on the stdout socket.  I am not sure how to read from the file object
after I perform the write into the process.  When I use "communicate" it
closes the PIPE and terminates the process.  Below are some configurations I

#  using the Popen.communicate API
k = Popen("/bin/bash", shell=True, bufsize=1024, stdin=PIPE, stdout=PIPE,
(stdout, stderr) = k.communicate("ls -all\n")
print stdout
<directory listing>

#  Trying to read k.stdout directly
k = Popen("/bin/bash", shell=True, bufsize=1024, stdin=PIPE, stdout=PIPE,
k.stdin.write("ls -all\n")
<hangs here>

On Mon, Jun 9, 2008 at 7:19 PM, Alan Gauld <alan.gauld at btinternet.com>

> "Demonic Software" <demonic.software at gmail.com> wrote
>  (cin, cout_cerror) = os.popen4("/bin/bash")
>> # write a command on the input pipe of the shell
>> print "writing ls -all!\n\n"
>> cin.write("ls -all")
>> cin.flush()
> I would have expected that you needed to pass an explicit newline at the
> end of the command?
> Just a guess.
> Also consider using the subprocess module and the Popen class.
> os.popen is somewhat deprecated nowadays.
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080609/72c14953/attachment.htm>

More information about the Tutor mailing list