when should I explicitly close a file?

Lawrence D'Oliveiro ldo at geek-central.gen.new_zealand
Sat Apr 17 07:23:15 EDT 2010


In message 
<d48b70da-5384-4dc6-9527-46c6b735ccc0 at r1g2000yqb.googlegroups.com>, gelonida 
wrote:

> I've been told, that following code snippet is not good.
> 
> open("myfile","w").write(astring) ...

I do that for reads, but never for writes.

For writes, you want to give a chance for write errors to raise an exception 
and alert the user, instead of failing silently, to avoid inadvertent data 
loss. Hence the explicit close.



More information about the Python-list mailing list