[Tutor] reading from stdin
Max Noel
maxnoel_fr at yahoo.fr
Tue Mar 1 23:20:14 CET 2005
On Mar 1, 2005, at 22:08, Nick Lunt wrote:
> The way I did this was to use sys.stdin.readlines() to get the output
> from the pipe.
>
> Here is the program:
>
> [code]
> import sys, glob
> args = sys.stdin.readlines() # found on the net
> pat = sys.argv[1]
> for i in args:
> if (i.find(pat) != -1):
> print i,
> [/code]
>
> My question is am I getting the output from the pipe in the correct
> way ? The way Im doing it works (so far) but should I be doing it
> another way ?
I don't think you are. You're using readlines(), which means your
program won't execute until ps terminates.
UNIX philosophy is to have programs start acting as soon as possible
-- in that case, as soon as the first line is available. You should be
reading sys.stdin as an iterator (same thing you'd do for a file):
import sys
for line in sys.stdin:
# do stuff with that line of input
-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting
and sweating as you run through my corridors... How can you challenge a
perfect, immortal machine?"
More information about the Tutor
mailing list