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


Groups > comp.lang.python > #103079

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

From Mark Lawrence <breamoreboy@yahoo.co.uk>
Newsgroups comp.lang.python
Subject Re: Will file be closed automatically in a "for ... in open..." statement?
Date 2016-02-18 02:05 +0000
Message-ID <mailman.228.1455761136.22075.python-list@python.org> (permalink)
References <acc702dc-dccd-49ca-857f-b256ad619123@googlegroups.com> <f5c53cc8-1009-4f5b-9d34-e78c981edec2@googlegroups.com>

Show all headers | View raw


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?

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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