subprocess query

Chris Rebert clp2 at rebertia.com
Sat Jul 3 10:39:40 EDT 2010


On Sat, Jul 3, 2010 at 7:33 AM, Sudheer <inbox1.sudheer at gmail.com> wrote:
> Hi,
>  What's wrong with the following code. The program waits indefenitely
> at  'output = p2.stdout.read()'
>
>
> from subprocess import *
>
> p1=Popen(['tr', 'a-z', 'A-Z'],stdin=PIPE,stdout=PIPE)
> p2=Popen(['tr','A-Z', 'a-z'],stdin=p1.stdout,stdout=PIPE)
> p1.stdin.write("hello")
> p1.stdin.close()
>
> output = p2.stdout.read()
>
> print output

Try using .communicate() instead of reading and writing to .stdin and .stdout.
Adding a newline (i.e. "hello\n") may also help.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list