[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
cmd='./listen.py'
or even if you are now in some one of PATH
cmd='listen.py
Maybe it hits the point. Just maybe, because you didn't give your output.
Here is an example code, it really works at least.
-----------foo.py--------------
import popen2
cmd='python listen.py'
(pout,pin)=popen2.popen2(cmd)
pin.write('Here is a message from pipe.')
pin.close()
got=pout.readlines()
pout.close()
print '\n'.join(got)
---------listen.py-----------
try:
uh=raw_input("Write this through pipe: ")
#Write this to stdin, so uh should have message
except EOFError:
print 'No message.'
else:
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")
>
>w.close()
>
>got = r.readlines()
>r.close()
>
>for line in got:
> print line
>
>---
>listen.py
>---
>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 ...
>
>cheers
>ktpr
>
>__________________________________________________
>Do You Yahoo!?
>Tired of spam? Yahoo! Mail has the best spam protection around
>http://mail.yahoo.com
>_______________________________________________
>Tutor maillist - Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>
>
>
More information about the Tutor
mailing list