aleax at aleax.it
Thu Aug 21 08:23:20 CEST 2003
David Bear wrote:
> I was in a hurry to do something and I did
> stuff = open("filename", "r").read()
> then I realized, "hey I didn't close the file".
> so python close it automagically? Since I was doing python
> interactively I expect the interpreter closed the file for me but my
> guess is the above syntax is probably poor. any comments?
It's very common use in classic Python, and sometimes handy
for interactive or near-one-liner use, but in any "serious" program
explicitly closing the file just as soon as you're done with it is
beter than relying on the interpreter closing it for you -- it will
make your code more usable in Jython, and taken as a habit
it will prevent some cases (not -- directly -- the above one) where
even in CPython you might hang on to resources longer than
necessary. try/final is the way to GUARANTEE that some key
finalization code gets executed, by the way, in case you need that.
More information about the Python-list