help with popen, one-way IPC.
Roeland Rengelink
r.b.rigilink at cable.a2000.nl
Mon Oct 2 02:48:34 EDT 2000
Brian Alexander wrote:
>
> Hello;
>
> I have a program that might possibly be writing to stdout (not a python
> program) and would like to use redirection in Python to collected the
> output. I'm having a little trouble with the call to popen(), though.
>
> I thought it was this:
>
> otherapp_stdout = popen( "appname", "r")
>
> while otherapp_stdout.readline != "\n"
> # do stuff with each line.
>
> Is this the right idea. Communication only needs to be one way.
>
> Many thanks in advance,
>
> Brian.
Hi Brian,
You're on the right track.
There are basically two ways to do this.
import os
otherapp_stdout = os.popen( "appname", "r")
lines = otherapp_stdout.readlines() # get all output at once
for line in lines:
print line
or:
import os
otherapp_stdout = os.popen( "appname", "r")
while 1:
line = otherapp_stdout.readline() # get them one at a time
if line == '': # an empty line means EOF
break
print line
Because assignment is a statement, there is no result that can be used
as a condition for while or if
Note the difference between readlines() (returning a list of lines)
and readline() (returning one line at a time).
Also note that an empty line denotes end-of-file. Don't confuse a blank
line (i.e. containing only the newline character '\n') for the empty
line marking EOF
Hope this helps.
Roeland
More information about the Python-list
mailing list