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


Groups > comp.lang.python > #103080

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

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
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 Thu, 18 Feb 2016 13:22:49 +1100
Lines 23
Message-ID <mailman.229.1455762179.22075.python-list@python.org> (permalink)
References <na38t6$vi6$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii; format=flowed
X-Trace news.uni-berlin.de 3HU9+ZO9QJtWOnHnTM8rKQ1wwjOszz61fSIa0nYlQ4Jg==
Return-Path <cameron@cskk.homeip.net>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'subject:file': 0.07; 'subject:skip:a 10': 0.09; 'exception': 0.13; '"for"': 0.16; '>on': 0.16; 'better?': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'simpson': 0.16; 'subject:closed': 0.16; 'wrote:': 0.16; 'lawrence': 0.22; 'cheers,': 0.22; 'somewhere': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; '(early': 0.29; 'etc).': 0.29; 'thread,': 0.29; "i'm": 0.30; 'certainly': 0.30; 'statement': 0.32; 'his/her': 0.33; 'open': 0.33; 'similar': 0.33; 'file': 0.34; 'handle': 0.34; 'skip:> 10': 0.35; 'but': 0.36; 'flow': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'charset:us-ascii': 0.37; 'received:localdomain': 0.38; 'sure': 0.39; 'enough': 0.39; 'to:addr:python.org': 0.40; 'mark': 0.40; 'some': 0.40; 'face': 0.64; 'cameron': 0.66
Content-Disposition inline
In-Reply-To <na38t6$vi6$1@ger.gmane.org>
User-Agent Mutt/1.5.24 (2015-08-30)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21rc2
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:103080

Show key headers only | View raw


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


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