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.

2
  • I know I can do this. I was wondering if there was a shortcut or some way to close all open files.
    – Confluence
    Commented Oct 22, 2012 at 17:26
  • 2
    @Confluence No, you cannot. The only thing Python (or any other GC'd language for that matter) can figure out automatically is when an object cannot possibly be in use anymore, and even then it may only notice that long after it becomes unreachable. Even if it could always detect unreachability immediately, that's still not (consider f = open(...); read_but_dont_close(f); do_a_lot_of_work()). Just get used to with. Even ignoring the technical benefits, it makes the period of use explicit, and explicit is better than implicit.
    – user395760
    Commented Oct 22, 2012 at 19:20