Hmmm, Beazley book missing something?

Bjorn Pettersen bjorn at roguewave.com
Tue Aug 1 17:27:11 EDT 2000


Steve Lamb wrote:
> 
>     Ok, trying to get a basic read loop going for stdin in Python.  If I may
> explain in perl.
> 
> while (<>){
>   do_meaningful_crap_here();
> }
> 
>     Look up stdin in the Beazley book and note that it has a basic routine for
> reading stdin character by character.  That can't be right.  So I check dir()
> on sys and then sys.stdin
> 
> >>> dir(sys.stdin)
> ['close', 'closed', 'fileno', 'flush', 'isatty', 'mode', 'name', 'read',
> 'readinto', 'readline', 'readlines', 'seek', 'softspace', 'tell',
> 'truncate', 'write', 'writelines']
> 
>     I see readline and readlines in there.  Only mention of it is in the
> multifile object.  No __doc__ string for those methods, either.  Did this get
> overlooked in this book?

The standard pydiom would be:

  while 1:
    line = sys.stdin.readline()
    if not line: break
    # do stuff here...

not-even-going-to-try-to-defend-it'ly y'rs
--bjorn




More information about the Python-list mailing list