looping over a big file
cam.ac.uk at mh391.invalid
Mon Jul 4 02:11:08 CEST 2005
Mike Meyer wrote:
> Roy Smith <roy at panix.com> writes:
>>The "right" way to do this is:
>>for line in file ("filename"):
>>The file object returned by file() acts as an iterator. Each time through
>>the loop, another line is read and returned (I'm sure there is some
>>block-level buffering going on at a low level).
> I disagree. That's the *convenient* way to do it, and perfectly
> acceptable in many situations. But not all Python interpreters will
> close the file when for loop ends. Likewise, if you get an exception
> during the processing, the file may not get closed properly. Those
> things may matter to you, in which case the "right" way is:
> data = open("filename")
> for line in data:
> Guido has made a pronouncement on open vs. file. I think he prefers
> open for opening files, and file for type testing, but may well be
> wrong. I don't think it's critical.
He has said that open() may be used for things other than files in the
future. So if you want to be sure you're opening a file, use file().
More information about the Python-list