[Tutor] do I need f.close()

W W srilyk at gmail.com
Tue Jun 10 19:12:38 CEST 2008


On Tue, Jun 10, 2008 at 12:07 PM, dave selby <dave6502 at googlemail.com> 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 ?

My guess is that's probably correct, but it's also bad programming.

It's like that old saying of my grandma's:
If you open it, close it!

-Wayne


More information about the Tutor mailing list