Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #103080
| From | Cameron Simpson <cs@zip.com.au> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: Will file be closed automatically in a "for ... in open..." statement? |
| Date | 2016-02-18 13:22 +1100 |
| Message-ID | <mailman.229.1455762179.22075.python-list@python.org> (permalink) |
| References | <na38t6$vi6$1@ger.gmane.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>
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Will file be closed automatically in a "for ... in open..." statement? Cameron Simpson <cs@zip.com.au> - 2016-02-18 13:22 +1100
csiph-web