The usual pattern for this is to use a generator, which automatically does the cleanup for you:

http://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects