[Tutor] do I need f.close()

Terry Carroll carroll at tjc.com
Thu Jun 12 21:17:26 CEST 2008


On Wed, 11 Jun 2008, dave selby wrote:

> The whole topic came up because I just finished reading 'learning
> python' 3rd edition OReilly as a refresher where there are multiple
> instances of suggesting that you do the exact opposite eg ...
> 
> [line.rstrip() for line in open('myfile')] ... p361
> for line in open('script1.py') ... p261& p276 where it is described as
> 'best practice' for reading files line by line
> etc ...

But that's for use of an input file.  The question was about writing.  I 
wouldn't worry too much about closing file that was used as input, as I am 
about one for output, where you want to be careful that the file contents 
are left in a particular desired state.



More information about the Tutor mailing list