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


Groups > comp.lang.python > #102995

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

From Cameron Simpson <cs@zip.com.au>
Newsgroups comp.lang.python
Subject Re: Will file be closed automatically in a "for ... in open..." statement?
Date 2016-02-16 20:24 +1100
Message-ID <mailman.158.1455614649.22075.python-list@python.org> (permalink)
References <acc702dc-dccd-49ca-857f-b256ad619123@googlegroups.com>

Show all headers | View raw


On 16Feb2016 00:39, jfong@ms4.hinet.net <jfong@ms4.hinet.net> wrote:
>I know
>
>    with open('foo.txt') as f:
>        ...do something...
>
>will close the file automatically when the "with" block ends.

Yes, because open is a context manager - they're great for reliably tidying up 
in the face of exceptions or "direct" departure from the block, such as a 
"return" statement.

>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?

This does not reliably close the file.

In CPython (the common implementation, and likely what you are using), objects 
are reference counted and when the interpreter notices their counter go to 
zero, the object's __del__ method is called before releasing the object's 
memory.

For an open file, __del__ _does_ call close if the file is open. However, only 
reference counting Pythons will call __del__ promptly - other systems rely on 
garbage collectors to detect unused objects.

In the for loop above, for interpreter obtains an iterator from the open file 
which returns lines of text. That iterator has a reference to the open file, 
and the for loop has a reference to the iterator. Therefore the file remains 
references while the loop runs. AT the end of the loop the iterator is 
discarded, reducing its references to zero. That in turn triggers releasing the 
open file, dropping its references to zero.

In CPython, that in turn will fire the open file's __del__, which will close 
the file. In other Pythons, not necessarily that promptly.

Also, there are plenty of ways to phrase this where the file reference doesn't 
go to zero. FOr example:

  f = open(...)
  for line in f:
    ...

You can see that it is easy to forget to close the file here (especially if you 
have an exception or exit the function precipitously).

Try to use the "with open(...) as f:" formulation when possible. It is much 
better.

Cheers,
Cameron Simpson <cs@zip.com.au>

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