[Tutor] (no subject)(About Pipe)

沈洁元 orion_val at 163.com
Thu Dec 30 04:28:33 CET 2004

Hello, KTPR
    I've read your code.  May the main problem locates at "cmd".  Assume 
that foo.py and listen.py are at the same directory.  It should look like:
cmd='python listen.py'
    or if you included '#!/usr/bin/python' (Linux) or 
'#!/usr/local/bin/python' (FreeBSD) as the first line in listen.py and 
you changed mode of listen.py to a+x
    or even if you are now in some one of PATH

Maybe it hits the point.  Just maybe, because you didn't give your output.
Here is an example code, it really works at least.
import popen2

cmd='python listen.py'
pin.write('Here is a message from pipe.')
print '\n'.join(got)

    uh=raw_input("Write this through pipe: ")
#Write this to stdin, so uh should have message
except EOFError:
    print 'No message.'
    print uh

    Juan Shen

ktpr wrote:

>Hi all,
>I am trying to supply input to another program using
>pipes. I can read from it just fine, but writing to
>its stdin doesn't seem to be working. 
>foo.py (gets pipe to listen.py)
>from popen2 import popen2
>cmd = "listen.py"
>#stdout, stdin
>r, w = popen2(cmd)
>w.write("Message sent")
>got = r.readlines()
>for line in got:
>    print line
>import sys
># wrote to this stdin, so uh should have message
>uh = raw_input("why doesn't it")
>if "" != uh:
>    print "go here?"
>if "" == uh:
>    print "or even here?"
>I know I'm missing something conceptual...
>I only get "why doesn't it" printed to the screen and
>the message didn't seem to transfer across the pipe,
>as listen.py aborted before the if statement somehow.
>Any and all help would be appreciated; popen2._test()
>works fine ...
>Do You Yahoo!?
>Tired of spam?  Yahoo! Mail has the best spam protection around 
>Tutor maillist  -  Tutor at python.org

More information about the Tutor mailing list