Populating a list
pzw1 at hotmail.com
Tue Dec 11 00:03:20 CET 2001
Chris Barker <chrishbarker at attbi.com> wrote in message news:<3C150599.33C3449B at attbi.com>...
> Kevin Cazabon wrote:
> > > f=open('/home/mlorfeld/states.txt', 'r+').readlines()
> > The file is still open
> > however, you just have no way to close it. I'd hope that Python would
> > close the file once it goes out of scope, but I couldn't guarantee it.
> Python does close the file when it's reference count goes to zero, which
> will happen as soon as that line is done processing. I'm pretty sure it
> is guaranteed, I certainly have never had a problem with it. That's
> what's nice about Python's reference counting scheme.
It's guaranteed to be deleted once the refcount reaches zero, but note
that if readlines() throws an exception, then a reference to the file
handle will still exist in the stack frame in the traceback object,
meaning that the file is not closed as long as the traceback object is
my take on things is that with sockets and files, it's better to be
neat and clean them up yourself, because then you know exactly what is
going on in your code, rather than relying upon the interpreter/gc
More information about the Python-list