iteration tool)but it is convenient in scripts that save output in list to be written later closing the file close method used earlier finalizes file contents and frees up system resources for instanceclosing forces buffered output data to be flushed out to disk normallyfiles are automatically closed when the file object is garbage collected by the interpreter (that iswhen it is no longer referencedthis includes all remaining open files when the python session or program exits because of thatclose calls are often optional in factit' common to see file-processing code in python in this idiomopen('somefile txt'' 'write(" 'day bruce\ "open('somefile txt'' 'read(write to temporary object read from temporary object since both these expressions make temporary file objectuse it immediatelyand do not save reference to itthe file object is reclaimed right after data is transferredand is automatically closed in the process there is usually no need for such code to call the close method explicitly in some contextsthoughyou may wish to explicitly close anyhowfor onebecause the jython implementation relies on java' garbage collectoryou can' always be as sure about when files will be reclaimed as you can in standard python if you run your python code with jythonyou may need to close manually if many files are created in short amount of time ( in loop)in order to avoid running out of file resources on operating systems where this matters for anothersome idessuch as python' standard idle guimay hold on to your file objects longer than you expect (in stack tracebacks of prior errorsfor instance)and thus prevent them from being garbage collected as soon as you might expect if you write to an output file in idlebe sure to explicitly close (or flushyour file if you need to reliably read it back during the same idle session otherwiseoutput buffers might not be flushed to disk and your file may be incomplete when read and while it seems very unlikely todayit' not impossible that this auto-close on reclaim file feature could change in future this is technically feature of the file object' implementationwhich may or may not be considered part of the language definition over time for these reasonsmanual close calls are not bad idea in nontrivial programseven if they are technically not required closing is generally harmless but robust habit to form ensuring file closureexception handlers and context managers manual file close method calls are easy in straight-line codebut how do you ensure file closure when exceptions might kick your program beyond the point where the close call is codedfirst of allmake sure you must--files close themselves when they are collectedand this will happen eventuallyeven when exceptions occur file tools