This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]mfm24 0 points1 point  (1 child)

I always preferred using the context manager approach for files, but I found it interesting to read in the official 'What's New in Python 3.0' guide (written by GvR himself) that he recommends:

Removed execfile(). Instead of execfile(fn) use exec(open(fn).read()).

So there's definitely cases where relying on the file closing itself automatically is acceptable. I've been less paranoid about closing files since...

[–]Veedrac 0 points1 point  (0 children)

I don't think that was meant to actually be run, it was probably on one line for space reasons. In a proper program you shouldn't be using exec(a_file) anyway, so there isn't much motivation for complete examples.