Newbie question: does file("text.txt', "w").write("stuff") ever get closed?

Fredrik Lundh fredrik at
Fri Nov 18 09:20:08 CET 2005

"John Doe" <spamproof at> wrote:

> In an effort to avoid another potential mistake, I am wondering if the
> anonymous file object/class/thingy that I create when I do file("text.txt',
> "w").write("stuff") gets closed properly on exit or garbage collection or
> something


> or if I always need to hang on to these things and close them myself.

only if you want to 1) know exactly when the files are closed, or 2) are
using so many files that the garbage collector cannot keep up with you
(this never happens on CPython, but can, at least in theory, happen on
other Python implementations)


More information about the Python-list mailing list