Opening files without closing them
Marcin Mielżyński
lopexx at autograf.pl
Sun Mar 5 17:45:12 EST 2006
Marcin Mielżyński wrote:
> 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
Oops I thought I was writing to c.l.ruby :D
sorry for spam
lopex
More information about the Python-list
mailing list