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


Groups > comp.lang.python > #103042

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 Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: Will file be closed automatically in a "for ... in open..." statement?
Date Wed, 17 Feb 2016 15:36:53 +1100
Lines 18
Message-ID <mailman.196.1455683816.22075.python-list@python.org> (permalink)
References <acc702dc-dccd-49ca-857f-b256ad619123@googlegroups.com> <230cfde5-e6f2-45fe-9739-e56c5c7739ab@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de b0CrAewE+NNv96EKns74MQgcwyrmFMC1FVx4k4y/OPAw==
Return-Path <rosuav@gmail.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; 'received:209.85.223': 0.03; 'subject:file': 0.07; 'cc:addr:python-list': 0.09; 'closed.': 0.09; 'exceptions,': 0.09; 'statements': 0.09; 'subject:skip:a 10': 0.09; 'underlying': 0.09; 'python': 0.10; 'wed,': 0.15; '"for"': 0.16; '"with"': 0.16; '2016': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'stuff,': 0.16; 'subject:closed': 0.16; 'wrote:': 0.16; 'later': 0.16; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'rid': 0.22; 'feb': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'right.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'object,': 0.27; 'loop,': 0.29; "can't": 0.32; 'know.': 0.34; 'file': 0.34; 'received:google.com': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'thanks': 0.37; 'sources': 0.37; 'received:209': 0.38; 'stuff': 0.38; 'does': 0.39; 'hang': 0.60; 'close': 0.61; 'hope': 0.61; 'skip:u 10': 0.61; 'better.': 0.66; "'for'": 0.84; 'chrisa': 0.84; 'to:none': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=7VmOCfQn/2ExtYKdmp7oPwVkQN6uZSFYygbSkz+HQrs=; b=Af1T1Ny2quzx8kKKpf2Nxwc5nmVVNFydF/q//Z7RlKUh61iRrJvmboxQ+gK9hmfcR2 a8SUeUcmfnPSdaFdRnLAC/NuwvdHevF8uwQfpHNfssXCi9hOBE8PTaujK6MUeiJLdp22 3bXZDnsniKNU2fl1X7iPpDzlbkEZTxr29XpZUI4Qg4WxhQDYOk7gVDrdooefyyeCJcc9 RnYDiUDUjatAOg8zMagiXhQT+z1qdSfpf7+xIofkSeAHPIAERd6eGrs3x0/Wdsi9w5We Z29EVyET53BV2c1IPP9AkzqwEBd3JFDdk4sQqmHPq1ehr6HetrtXgRezQHWKOLV8CBXR sZfw==
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc:content-type; bh=7VmOCfQn/2ExtYKdmp7oPwVkQN6uZSFYygbSkz+HQrs=; b=G40tDkx46A7d8S5qjWjBNNCFT1mcz5iaFNP6WdiMlQV1yxVpjU8lu4zPTlV5NNNgwG Tfocaz8nrIZdughr+Hiq4c+q4z0EIZevb2O6wwWgsTwq8206qN6DeseuSlyPNzIbsiJ7 KqnEd+JyaHz5jq5z4K4ZKiJv4vZafqf5SNg+RNsc5lOykfbaBalMEAakBDUKi2SxxOs6 cwNAI2F5CLlT1Vt5y23BIsnco36Ecp6ANs1UlRFZWPq90iEc1++e9PBfcz8LvGtyL3k4 iOm2s+a0z23eptqcx/GmysThDSJaUOXNcBWGTPHLagvY0LYDGlNsw4AIiCj3pLE/U5fo 3T9g==
X-Gm-Message-State AG10YORhEKBBz5fvY3XRyxfqo5aRaqXPNUQ+y9W9koJJkj6IvwHHAY84IlH0aqu1RsQ2Tr6vSGsEi0pBlLLhmw==
X-Received by 10.107.47.162 with SMTP id v34mr1111809iov.19.1455683813209; Tue, 16 Feb 2016 20:36:53 -0800 (PST)
In-Reply-To <230cfde5-e6f2-45fe-9739-e56c5c7739ab@googlegroups.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:103042

Show key headers only | View raw


On Wed, Feb 17, 2016 at 3:04 PM,  <jfong@ms4.hinet.net> wrote:
> Thanks for these detailed explanation. Both statements will close file automatically sooner or later and, when considering the exceptions, "with" is better. Hope my understanding is right.
>
> But, just curious, how do you know the "for" will do it? I can't find any document about it from every sources I know. Very depressed:-(
>

It's not the 'for' loop that does it. The for loop is kinda like this:

_temp = open("foo.txt")
_temp.read() # do stuff, do stuff
_temp = None

When you stop holding onto an object, Python can get rid of it. When
that happens is not promised, though - and if you have a reference
loop, it might hang around for a long time. But when a file object is
disposed of, the underlying file will get closed.

ChrisA

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