I'm an idiot

Joseph A Knapka jknapka at earthlink.net
Sat Jun 29 03:06:25 EDT 2002


"Beej Jørgensen" wrote:
> 
> In article <Xns923BB72B1BAF7davidgriswold1netsca at 204.127.202.16>,
> David  <david_griswold1 at yahoo.com> wrote:
> >Any help would be appreciated.
> 
> Ok, I'm no pro and don't have the manuals in front of me, so I hope I
> don't lead you too astray here:
> 
> >f=open('c:\\temp\\temp.txt', 'r')
> >g=open('c:\\temp\\temp1.txt', 'w')
> >while 1:
> >    try:
> >        s=f.readline
>              ^^^^^^^^
> 
> This is a method, so I think it should be "f.readline()"
> 
> >        g.write(s.split())
> 
> Er, did you mean strip()?  And maybe you have to use String.strip(s),
> but I can't remember.
> 
> >    except IOError:
> >        break
> 
> 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 :-)

Cheers,

-- Joe



More information about the Python-list mailing list