[Tutor] reading from stdin
Sean Perry
shaleh at speakeasy.net
Tue Mar 1 23:14:39 CET 2005
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 ?
>
unless you want the output for some other reason, a more idiomatic way
is:
for line in sys.stdin.readlines():
# handle the line
I tend to use xreadlines() which does not read the entire input at once.
For stdin this make sense, you have no idea how much data will be
piped in.
More information about the Tutor
mailing list