Does a File Object Automatically Close When Its Reference Count Hits Zero

Does a File Object Automatically Close when its Reference Count Hits Zero?

The answer is in the link you provided.

Garbage collector will close file when it destroys file object, but:

  • you don't really have control over when it happens.

    While CPython uses reference counting to deterministically release resources
    (so you can predict when object will be destroyed) other versions don't have to.
    For example both Jython or IronPython use JVM and .NET garbage collector which
    release (and finalize) objects only when there is need to recover memory
    and might not do that for some object until the end of the program.
    And even for CPython GC algorithm may change in the future as reference counting
    isn't very efficient.

  • if exception is thrown when closing file on file object destruction,
    you can't really do anything about it because you won't know.

Do files get closed during an exception exit?

No, they don't.

Use with statement if you want your files to be closed even if an exception occurs.

From the docs:

The with statement is used to wrap the execution of a block with
methods defined by a context manager. This allows common
try...except...finally usage patterns to be encapsulated for convenient reuse.

From docs:

 The with statement allows objects like files to be used in a way that ensures they are always cleaned up promptly and correctly.

with open("myfile.txt") as f:
    for line in f:
        print line,

After the statement is executed, the file f is always closed, even if a problem was encountered while processing the lines. Other objects which provide predefined clean-up actions will indicate this in their documentation.

How to cause an error or unwanted behaviour by not closing a file object in python? (code example pls)

Are you asking if Python will raise an error if you fail to close a file? Then the answer is "no".

If you are asking if you might lose data, the answer is "yes".

By analogy, will the cops write you a ticket if you leave your keys in the ignition? No.
Does this practice increase the odds that you will "lose" your car? Yes.

Edit:

Ok, you asked for an example, not smart-aleck comments. Here is an example, although a bit contrived because it's easier to do this than investigate buffer-size corner cases.

Good:

fh = open("erase_me.txt", "w")
fh.write("Hello there!")
fh.close()

# Writes "Hello there!" to 'erase_me.txt'
# tcsh-13: cat erase_me.txt
# Hello there!tcsh-14:

Bad:

import os
fh = open("erase_me.txt", "w")
fh.write("Hello there!")

# Whoops! Something bad happened and my program bombed!
os._exit(1)

fh.close()

# tcsh-19: cat erase_me.txt
# tcsh-20: ll erase_me.txt
# -rw-r--r-- 1 me us 0 Jul 17 15:41 erase_me.txt
# (Notice file length = 0)

how soon is `__del__` called after reference count drops to zero?

Python doesn't make any guarantees about when __del__ is called, or whether it is called at all. As it is, __del__ methods are unlikely to be called if the object is part of a reference cycle, because even if the cycle as a whole is cleaned up, Python has no way to decide where to break the cycle and in what order the __del__ methods (if any) should be called. Because of __del__'s rather quirky semantics (in order to call __del__ the refcount of the object is temporarily increased, and the __del__ method can prevent destruction of the object by storing the reference somewhere else) what happens in other implementations is a bit of a crapshoot. (I don't remember the exact details in current Jython, but it has changed a few times in the past.)

That said, in CPython, if __del__ is called, it's called as soon as the reference count drops to zero (since refcounting is the only way __del__ methods are called, and the only chance CPython has of calling __del__ is when the actual refcount is changed.)

Do I have to use close() if I used open() as argument in function?

You don't have to close the file, but it my result in unexpected behavior and/or memory leak if the program continues. Python provides the with statement to close it automatically

with open("data.json", 'r') as f:
data = json.load(f)

Is file closing necessary in this situation?

Use

with open(pathf, "r") as r:
fdata = r.read().splitlines()
# as soon as you leave the with-scope, the file is autoclosed, even if exceptions happen.

Its not only about auto-closing, but also about correct closing in case of exceptions.

Doku: methods of file objects

It is good practice to use the with keyword when dealing with file
objects. The advantage is that the file is properly closed after its
suite finishes, even if an exception is raised at some point. Using
with is also much shorter than writing equivalent try-finally blocks:

If you’re not using the with keyword, then you should call f.close()
to close the file and immediately free up any system resources used by
it.

If you don’t explicitly close a file, Python’s garbage collector
will eventually destroy the object and close the open file for you,
but the file may stay open for a while. Another risk is that different
Python implementations will do this clean-up at different times.



Related Topics



Leave a reply



Submit