Allowing ref counting to close file items bad style?

BJörn Lindqvist bjourne at gmail.com
Thu Aug 31 04:25:17 EDT 2006


On 8/30/06, Dan <bounces at foo.org> wrote:
> Is this discouraged?:
>
>      for line in open(filename):
>          <do something with line>

In theory, it is. In practice, that is the way Python code is written
because it more natural and to the point. Not just for hacked together
scripts, lots of third party modules includes code like "data =
open(filename).read()" and similar idioms.

> Is my data safer if I explicitly close, like this?:
>      fileptr = open("the.file", "w")
>      foo_obj.write(fileptr)
>      fileptr.close()

Have you ever experienced a problem caused by not explicitly closing
your file handles?

-- 
mvh Björn



More information about the Python-list mailing list