Is close() necessary when using iterator on a Python file object [duplicate]
Is it bad practice to do the following and not explicitly handle a file object and call its close()
method?
for line in open('hello.txt'):
print line
NB - this is for versions of Python that do not yet have the with
statement.
I ask as the Python documentation seems to recommend this :-
f = open("hello.txt")
try:
for line in f:
print line
finally:
f.close()
Which seems more verbose than necessary.
Close is always necessary when dealing with files, it is not a good idea to leave open file handles all over the place. They will eventually be closed when the file object is garbage collected but you do not know when that will be and in the mean time you will be wasting system resources by holding to file handles you no longer need.
If you are using Python 2.5 and higher the close()
can be called for you automatically using the with
statement:
from __future__ import with_statement # Only needed in Python 2.5
with open("hello.txt") as f:
for line in f:
print line
This is has the same effect as the code you have:
f = open("hello.txt")
try:
for line in f:
print line
finally:
f.close()
The with
statement is direct language support for the Resource Acquisition Is Initialization idiom commonly used in C++. It allows the safe use and clean up of all sorts of resources, for example it can be used to always ensure that database connections are closed or locks are always released like below.
mylock = threading.Lock()
with mylock:
pass # do some thread safe stuff
Actually, the file will be closed when it is garbage collected. See this question for more on how that works.
It is still recommended that you use a try
/finally
block or a with
statement though. If there is an exception when using one of the file object's methods, a reference will be stored in the traceback (which is stored as a global variable) until you clear it or another exception occurs.
Thus, it's bad to rely on garbage collection to close your file for you.
Also, if you've written to the file, you can't guarantee that the changes will be saved to the file until it is closed or flushed.
Strange that for all the discussion in this topic of the importance of freeing system resources, nobody has mentioned what seems to me an obviously more significant reason to close a file deterministically: so that it can be opened again.
There are certainly cases where it doesn't matter. If a file object goes out of scope or gets deleted, the underlying file will get closed. (When it gets closed depends on the specific implementation of Python you're using.) That's generally going to be good enough - if you know exactly when the file variable is going to go out of scope, and if you know that you don't care if the file gets closed deterministically.
But why should you even be troubling yourself with that kind of analysis when the with
statement exists?