I'm an idiot

Jim jbublitzNO at SPAMnwinternet.com
Sat Jun 29 04:42:56 EDT 2002


Joseph A Knapka wrote:
> "Beej Jørgensen" wrote:

>>Close--but I don't believe readline() throws an exception on EOF;
>>instead it just returns "".

> Now, personally, I'd like to have any read after EOF throw
> an exception. That's a nice way to break out of a loop, and
> it removes the confusion that newbies and occasionally
> oldbies (cough) sometimes experience: "If it returns '' at
> EOF, what the heck does it return for empty lines???".

> But I'm sure there are Very Good Reasons not to do this,
> and that some wise person will presently educate me :-)

It's the readline() method, meaning it reads/returns
lines, which are everything up to and including a
newline. In a text file (where this is meant to be
used), an 'empty' line contains at least a newline,
which is what readline() will return (not ''). The
only place an 'empty line' can exist is after the
final newline or after the last character after the
final newline for an unterminated last line. By
definition, that's EOF.

That's not a good explanation of why it doesn't throw
an exception - just an explanation of how readline()
works and why the exception isn't necessary. Generally,
reading past EOF isn't considered an error in C/C++
either. Look at read() or readline() in C for example.
If you want readline() to throw an exception, you can
define your own comparable function that does that.


Jim




More information about the Python-list mailing list