Opening files without closing them
Marcin Mielżyński
lopexx at autograf.pl
Sun Mar 5 17:44:05 EST 2006
Sandra-24 wrote:
> I was reading over some python code recently, and I saw something like
> this:
>
> contents = open(file).read()
>
> And of course you can also do:
>
> open(file, "w").write(obj)
>
> Why do they no close the files? Is this sloppy programming or is the
> file automatically closed when the reference is destroyed (after this
> line)? I usually use:
>
> try:
> f = open(file)
> contents = f.read()
> finally:
> f.close()
>
this above is equivalent to:
open(file){|f|
contents=f.read
}
the logic taking care of everything is encapsulated in open.
but can be done in less ruby way way :)
lopex
More information about the Python-list
mailing list