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


Groups > comp.lang.python > #103081

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 Tim Chase <python.list@tim.thechases.com>
Newsgroups comp.lang.python
Subject Re: Will file be closed automatically in a "for ... in open..." statement?
Date Wed, 17 Feb 2016 20:32:59 -0600
Lines 29
Message-ID <mailman.230.1455763407.22075.python-list@python.org> (permalink)
References <acc702dc-dccd-49ca-857f-b256ad619123@googlegroups.com> <230cfde5-e6f2-45fe-9739-e56c5c7739ab@googlegroups.com> <56c40a67$0$11104$c3e8da3@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de Fpzrs+T3ZOx4E2hSCQXB0AclPb6zXbiff9guaxNAcyzQ==
Return-Path <python.list@tim.thechases.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'finally:': 0.05; 'f.close()': 0.07; 'subject:file': 0.07; 'must:': 0.09; 'subject:skip:a 10': 0.09; ':-)': 0.12; 'explicitly': 0.15; '"with"': 0.16; '-tkc': 0.16; 'close()': 0.16; 'correctly,': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'messy': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sadly': 0.16; 'subject:closed': 0.16; 'wrote:': 0.16; 'try:': 0.18; 'code.': 0.23; 'header:In- Reply-To:1': 0.24; 'looks': 0.29; 'code': 0.30; '(i.e.,': 0.30; 'regardless': 0.31; "d'aprano": 0.33; 'steven': 0.33; 'maintaining': 0.34; 'file': 0.34; 'but': 0.36; 'lines': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'charset:us-ascii': 0.37; 'to:addr:python.org': 0.40; 'still': 0.40; 'received:46': 0.63; 'statement,': 0.66; 'received:10.235': 0.84; 'cost,': 0.91
X-Sender-Id wwwh|x-authuser|tim@thechases.com
X-Sender-Id wwwh|x-authuser|tim@thechases.com
X-MC-Relay Neutral
X-MailChannels-SenderId wwwh|x-authuser|tim@thechases.com
X-MailChannels-Auth-Id wwwh
X-MC-Loop-Signature 1455762950296:2046713923
X-MC-Ingress-Time 1455762950296
In-Reply-To <56c40a67$0$11104$c3e8da3@news.astraweb.com>
X-Mailer Claws Mail 3.11.1 (GTK+ 2.24.25; x86_64-pc-linux-gnu)
Importance high
X-Priority 1 (Highest)
X-AuthUser tim@thechases.com
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:103081

Show key headers only | View raw


On 2016-02-17 16:51, Steven D'Aprano wrote:
> If you want the file to be closed immediately, you must:
> 
> - use a with statement;
> 
> - or explicitly call f.close()

I have a lot of pre-"with" code (i.e., Py2.4) that looks like

  f = open(...)
  try:
    do_stuff()
  finally:
    f.close()

To explicitly close() correctly, you still have to pay the
one-level-of-indent cost regardless of whether you use a "with" or
close()

Now that we have the "with" statement, it's the same cost, with no
lost functionality, but with fewer lines of messy code.

So use the "with" unless you're sadly maintaining 2.4 code like
me. :-)

-tkc


Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Will file be closed automatically in a "for ... in open..." statement? jfong@ms4.hinet.net - 2016-02-16 00:39 -0800
  Re: Will file be closed automatically in a "for ... in open..." statement? Chris Angelico <rosuav@gmail.com> - 2016-02-16 20:16 +1100
  Re: Will file be closed automatically in a "for ... in open..." statement? Cameron Simpson <cs@zip.com.au> - 2016-02-16 20:24 +1100
  Re: Will file be closed automatically in a "for ... in open..." statement? Terry Reedy <tjreedy@udel.edu> - 2016-02-16 04:41 -0500
  Re: Will file be closed automatically in a "for ... in open..." statement? jfong@ms4.hinet.net - 2016-02-16 20:04 -0800
    Re: Will file be closed automatically in a "for ... in open..." statement? Chris Angelico <rosuav@gmail.com> - 2016-02-17 15:36 +1100
    Re: Will file be closed automatically in a "for ... in open..." statement? Raspberry Aether <raspberryaether@e-s.invalid> - 2016-02-16 23:42 -0500
    Re: Will file be closed automatically in a "for ... in open..." statement? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-02-17 16:51 +1100
      Re: Will file be closed automatically in a "for ... in open..." statement? Tim Chase <python.list@tim.thechases.com> - 2016-02-17 20:32 -0600
  Re: Will file be closed automatically in a "for ... in open..." statement? jfong@ms4.hinet.net - 2016-02-17 17:29 -0800
    Re: Will file be closed automatically in a "for ... in open..." statement? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-02-18 02:05 +0000
  Re: Will file be closed automatically in a "for ... in open..." statement? Jeremy Leonard <jrmy.lnrd@gmail.com> - 2016-02-18 10:02 -0800

csiph-web