Need a little parse help

Mike Meyer mwm at mired.org
Tue May 10 20:39:45 EDT 2005


"Michael Hartl" <mhartl at post.harvard.edu> writes:

> I'd also like to note that both the inputfiles variable and the
> readlines() method are superfluous; to iterate through the file line by
> line, use either
>
> for line in open(inputfilename):
>     # do something with line
>
> or (my personal preference, since I like to think of the file as a
> thing rather than an action)
>
> for line in file(inputfilename):
>     # do something with line

I'd like to note that failing to close the file explicitly is a bad
habit. You really should invoke the close method, rather than relying
on the garbage collector to close them for you. This means you do need
a variable to hold the file object. 99% of the time nothing bad will
happen if you skip this, but that 1% can always come back to byte you
in the *ss.

     <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list