by defaultoutput files are always bufferedwhich means that text you write may not be transferred from memory to disk immediately--closing fileor running its flush methodforces the buffered data to disk you can avoid buffering with extra open argumentsbut it may impede performance python files are also randomaccess on byte offset basis--their seek method allows your scripts to jump around to read and write at specific locations close is often optionalauto-close on collection calling the file close method terminates your connection to the external filereleases its system resourcesand flushes its buffered output to disk if any is still in memory as discussed in in python an object' memory space is automatically reclaimed as soon as the object is no longer referenced anywhere in the program when file objects are reclaimedpython also automatically closes the files if they are still open (this also happens when program shuts downthis means you don' always need to manually close your files in standard pythonespecially those in simple scripts with short runtimesand temporary files used by single line or expression on the other handincluding manual close calls doesn' hurtand may be good habit to formespecially in long-running systems strictly speakingthis auto-closeon-collection feature of files is not part of the language definition--it may change over timemay not happen when you expect it to in interactive shellsand may not work the same in other python implementations whose garbage collectors may not reclaim and close files at the same points as standard cpython in factwhen many files are opened within loopspythons other than cpython may require close calls to free up system resources immediatelybefore garbage collection can get around to freeing objects moreoverclose calls may sometimes be required to flush buffered output of file objects not yet reclaimed for an alternative way to guarantee automatic file closesalso see this section' later discussion of the file object' context managerused with the with/as statement in python and files in action let' work through simple example that demonstrates file-processing basics the following code begins by opening new text file for outputwriting two lines (strings terminated with newline marker\ )and closing the file laterthe example opens the same file again in input mode and reads the lines back one at time with read line notice that the third readline call returns an empty stringthis is how python file methods tell you that you've reached the end of the file (empty lines in the file come back as strings containing just newline characternot as empty stringshere' the complete interactionmyfile open('myfile txt'' 'myfile write('hello text file\ ' open for text outputcreate/empty write line of textstring files