Skip to content Skip to sidebar Skip to footer

Opened File Descriptors In Python

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

Solution 1:

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:

withopen('file') as fp:
    data = fp.read()

Solution 2:

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.

Solution 3:

The default Python implementation uses both garbage collection and reference counting. In your first example the file object's reference count drops to zero and so it is immediately closed even before the garbage collector runs.

The second version is equivalent to this:

_ = open('file')

Since the file is still referenced by _ it is still live until you run another command.

Note that this behaviour is specific to CPython. Other implementations such as IronPython may not be so fast to close the file so you should really close your files when you are finished using them. A nice way of doing this is by using a with statement.

withopen('file') as f:
    data = f.read()

Related

Post a Comment for "Opened File Descriptors In Python"