Mike Meyer wrote: > Roy Smith <[EMAIL PROTECTED]> writes: > >>The "right" way to do this is: >> >>for line in file ("filename"): >> whatever >> >>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") > try: > for line in data: > whatever > finally: > data.close() > > 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(). <wink> -- Michael Hoffman -- http://mail.python.org/mailman/listinfo/python-list