[Tutor] reading from stdin

Nick Lunt nick at javacat.f2s.com
Wed Mar 2 08:59:33 CET 2005


Thanks to everyone who helped me with this.
It's certainly given me something to think about :)

Cheers
Nick .


On Tue, 2005-03-01 at 23:13 -0600, David Rock wrote:
> * Nick Lunt <nick at javacat.f2s.com> [2005-03-01 22:23]:
> > On Tue, 2005-03-01 at 14:14 -0800, Sean Perry wrote:
> > 
> > > 
> > > 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.
> > 
> > Thanks Sean, I agree with you on both accounts there.
> 
> For another approach to this, I like to use the fileinput module. In the
> case of the original example of mimicing grep, it allows you to easily
> handle both a list of files passed to the command or use it as a pipe.
> The default action if there are no files given is to use stdin.
> 
> http://www.python.org/doc/2.4/lib/module-fileinput.html
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list