Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #103080 > unrolled thread

Re: Will file be closed automatically in a "for ... in open..." statement?

Started byCameron Simpson <cs@zip.com.au>
First post2016-02-18 13:22 +1100
Last post2016-02-18 13:22 +1100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Will file be closed automatically in a "for ... in open..." statement? Cameron Simpson <cs@zip.com.au> - 2016-02-18 13:22 +1100

#103080 — Re: Will file be closed automatically in a "for ... in open..." statement?

FromCameron Simpson <cs@zip.com.au>
Date2016-02-18 13:22 +1100
SubjectRe: Will file be closed automatically in a "for ... in open..." statement?
Message-ID<mailman.229.1455762179.22075.python-list@python.org>
On 18Feb2016 02:05, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote:
>On 18/02/2016 01:29, jfong@ms4.hinet.net wrote:
>>The "for ... open ..." is definitely not a good design pattern. It opens a file at "for" block but leaves it closed somewhere in the sky.
>>
>
>Hardly, as all ready explained, but how about this
>
>handle = open('foo.txt')
>for line in handle :
>    ...do something...
>handle.close()
>
>Is that better?

As already discussed in this thread, not in the face of an exception inside the 
loop or some other change of flow control (early "return" statement etc).

I'm pretty sure jfong understands the issues now, certainly enough to make 
his/her own decisions on the benefits and risks of with versus "for ... open" 
and similar variants.

Cheers,
Cameron Simpson <cs@zip.com.au>

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web