when I use this code in IPython3 shell
>>>data = open('file').read()
and then check open file descriptors:
lsof | grep file
I find empty list
and when I use this:
>>>open('file')
lsof
shows two items. The question is why first operation closes fd while second doesn't? I supposed that garbage collector must delete file object with no refs.
I know about '_' var in interpreter, when I reassign value
>>>111
>>>_
111
but descriptors remain open. when I repeat
>>>open('file')
n times there are 2 * n opened descriptors
In the second example the file handle is retained by the interactive interpreter variable _
, which allows you to access the last evaluated expression. If you evaluate another expression, such as 1+1
, you will note that the file is no longer reported by lsof
as open.
As pointed out by Mike Byers, this behavior is specific to CPython, and even then to precise circumstances of how the file object is used. To make sure the file is closed regardless of how the code is executed, use a with
statement:
with open('file') as fp:
data = fp.read()
This is because the interactive interpreter that you are using keeps an implicit reference to the last object returned. That reference is named _
.
Python2> open("/etc/hosts")
<open file '/etc/hosts', mode 'r' at 0xf2c390>
Python2> _
<open file '/etc/hosts', mode 'r' at 0xf2c390>
So it is still "alive" when you look at it. Do something else:
Python2> max(0,1)
1
And the file is now closed as it is no longer referenced.
But this is a good example of why you should explicitly close files that you really want closed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With