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


Groups > comp.lang.python > #103078

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

X-Received by 10.107.184.195 with SMTP id i186mr6131954iof.24.1455758957636; Wed, 17 Feb 2016 17:29:17 -0800 (PST)
X-Received by 10.50.78.169 with SMTP id c9mr10352igx.5.1455758957606; Wed, 17 Feb 2016 17:29:17 -0800 (PST)
Path csiph.com!feeder.erje.net!2.us.feeder.erje.net!news.glorb.com!hb3no6423743igb.0!news-out.google.com!l1ni17011igd.0!nntp.google.com!hb3no6423732igb.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.python
Date Wed, 17 Feb 2016 17:29:17 -0800 (PST)
In-Reply-To <acc702dc-dccd-49ca-857f-b256ad619123@googlegroups.com>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=36.238.70.173; posting-account=G2sM6AoAAADOlDdo9rWD6sFkj3T5ULsz
NNTP-Posting-Host 36.238.70.173
References <acc702dc-dccd-49ca-857f-b256ad619123@googlegroups.com>
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <f5c53cc8-1009-4f5b-9d34-e78c981edec2@googlegroups.com> (permalink)
Subject Re: Will file be closed automatically in a "for ... in open..." statement?
From jfong@ms4.hinet.net
Injection-Date Thu, 18 Feb 2016 01:29:17 +0000
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Xref csiph.com comp.lang.python:103078

Show key headers only | View raw


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.

> The garbage collector will:
> - reclaim the memory used by the object;
> - close the file. 

I suppose (IMO) that the primitive idea of garbage collection doing the file-close affair is to make itself a final defense to prevent the possible disaster. (after all it's not of his business:-) But some coding may abuse this advantage and forget its hidden danger.

It's better to drop this "for...open..." style completely and stick at "with".

Thanks for all you gurus clarify my puzzle. I deeply appreciated it.

--Jach

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