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


Groups > comp.lang.python > #103143

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

Newsgroups comp.lang.python
Date 2016-02-18 10:02 -0800
References <acc702dc-dccd-49ca-857f-b256ad619123@googlegroups.com>
Message-ID <cf098d8d-abd9-49df-8dce-3285ee88fd40@googlegroups.com> (permalink)
Subject Re: Will file be closed automatically in a "for ... in open..." statement?
From Jeremy Leonard <jrmy.lnrd@gmail.com>

Show all headers | View raw


On Tuesday, February 16, 2016 at 3:39:34 AM UTC-5, jf...@ms4.hinet.net wrote:
> I know
> 
>     with open('foo.txt') as f:
>         ...do something...
> 
> will close the file automatically when the "with" block ends. 
> 
> I also saw codes in a book:
> 
>     for line in open('foo.txt'):
> 	...do something...
> 
> but it didn't mention if the file will be closed automatically or not when the "for" block ends. Is there any document talking about this? and how to know if a file is in "open" or not?
> 
> --Jach Fong

One way of having a file automatically closed is to have something like:

with open("file_name") as foo:
    for x in foo:
       # process line x

Once the with block has completed, the file will be automatically closed. Sorry if this has already been posted, but I wasn't able to see it.

Back to comp.lang.python | Previous | NextPrevious 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