On Tue, Jun 10, 2008 at 10:07 AM, dave selby <[EMAIL PROTECTED]> wrote:
> Hi All, > > Up to now I when I need to write some data to a file I have been > purposely using close() > > f = open(conf, 'w') > f.writelines(lines) > f.close() > > Is it as safe to use the following .... > > open(conf, 'w').writelines(lines) > > ie no close() to flush the data, but also not assigned an object name > so am I right in thinking that as the object is 'reclaimed' close() is > automatically called ? > > Cheers > > Dave > > If you're using Python 2.5, you can use the new "with" - > with open("x.txt") as f: > data = f.read() > do something with data > > which takes care of closing and possible exception handling automagically. Fredrik Lundh has a good article about it: http://effbot.org/zone/python-with-statement.htm -- www.fsrtechnologies.com
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor