reading line by line

Michael P. Soulier msoulier at storm.ca
Sun Jan 14 08:46:16 EST 2001


    Hey people. I know that the readlines() method in file objects allows line
by line reading, but it seems to read the entire file first, which is far too
memory intensive if you're reading a massive file. Now, I know that you can do
this:

file = open(filename, "r")
line = file.readline()
while line:
    # work on file
    line = file.readline()

    But I don't like the initial readline() call to initialize line. It just
doesn't look clean to me. Is there a better way, syntactically, to do this?

    Thanks,

    Mike

-- 
Michael P. Soulier <msoulier at storm.ca>
"...the word HACK is used as a verb to indicate a massive amount
of nerd-like effort."  -Harley Hahn, A Student's Guide to UNIX
PGP Public Key: http://24.43.42.96/email.phtml



More information about the Python-list mailing list