T wrote: > Thomas Bartkus wrote: >> "T" <[EMAIL PROTECTED]> wrote in message >> news:[EMAIL PROTECTED] >>> Do I need to close the file in this case? Why or why not? >>> >>> for line in file('foo', 'r'): >>> print line >> Are you asking if you can get away without closing it? >> Or are you asking if it is a good idea to not close it? >> >> Good programming practice says that if you open it - you close it. >> >> And stay out of trouble ;-) >> Thomas Bartkus > > How do I close the file in the above case?
You rewrite the faulty code such that the above case isn't the above case anymore. f = open('foo', 'r') try: for line in f: print line finally: f.close() -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list