Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

7
  • 2
    -1 There's no guarantee that the cleanup action will happen immediately (it depends on which implementation of Python you're using). Commented Jul 27, 2010 at 21:00
  • 2
    The del statement only removes the name binding: "Deletion of a name removes the binding of that name from the local or global namespace, depending on whether the name occurs in a global statement in the same code block." It doesn't say anything about when cleanup might happen. Commented Jul 27, 2010 at 21:04
  • @James Roth, while this probably will work for CPython, it is a really bad idea. It won't work for Jython for example as writer will not be closed until it is gc'd. Python says nothing about when the file would be closed by deleting the reference to writer. More importantly though it's disguising the intent, which is to close the file which harms readability Commented Jul 27, 2010 at 21:38
  • oy vey james this is really a horrific idea indeed, horrificable Commented Jul 27, 2010 at 22:28
  • 2
    +1 Only answer that fixes my problem. All the others are retroactive (i.e. here's what you should do in the future). Should be noted that you should call gc.collect() immediately afterward for manually starting garbage collection. Commented Aug 23, 2012 at 21:17