an ugly file-reading pattern

Matt Gerrans mgerrans at mindspring.com
Sat Apr 12 22:49:08 EDT 2003


Learning Python is an excellent book, but unfortunately quite out of date.
It covers Python version 1.5, whereas 2.2.2 is the current release, with 2.3
in alpha.

Anyway, the syntax to read lines from a file without loading the whole thing
is now quite simple and clean:

for line in file( filename ):
   ... do your thing ...

or if you prefer open():

for line in open( filename ):
   ... do your thing ...






More information about the Python-list mailing list