Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is close() necessary when using iterator on a Python file object [duplicate]

Is it bad practice to do the following and not explicitly handle a file object and call its close() method?

for line in open('hello.txt'):
    print line

NB - this is for versions of Python that do not yet have the with statement.

I ask as the Python documentation seems to recommend this :-

f = open("hello.txt")
try:
    for line in f:
        print line
finally:
    f.close()

Which seems more verbose than necessary.

like image 755
Dave M. Avatar asked Dec 02 '09 12:12

Dave M.


People also ask

Is it mandatory to call the close () method after using the file?

Any operation, which requires that the file be opened will raise a ValueError after the file has been closed. Calling close() more than once is allowed. Python automatically closes a file when the reference object of a file is reassigned to another file. It is a good practice to use the close() method to close a file.

Why do we need close () in Python?

You've learned why it's important to close files in Python. Because files are limited resources managed by the operating system, making sure files are closed after use will protect against hard-to-debug issues like running out of file handles or experiencing corrupted data.

What is the purpose of using close () on a file you have written to?

When you write to a file using write() system call, it doesn't writes immediately to the file. So after your operations, you have to call close() so that the buffer is flushed to the file and changes persist.

Do you always have to close a file in Python?

Though Python automatically closes a file if the reference object of the file is allocated to another file, it is a standard practice to close an opened file as a closed file reduces the risk of being unwarrantedly modified or read. Python has a close() method to close a file.


3 Answers

Close is always necessary when dealing with files, it is not a good idea to leave open file handles all over the place. They will eventually be closed when the file object is garbage collected but you do not know when that will be and in the mean time you will be wasting system resources by holding to file handles you no longer need.

If you are using Python 2.5 and higher the close() can be called for you automatically using the with statement:

from __future__ import with_statement # Only needed in Python 2.5
with open("hello.txt") as f:
    for line in f:
        print line

This is has the same effect as the code you have:

f = open("hello.txt")
try:
    for line in f:
        print line
finally:
    f.close()

The with statement is direct language support for the Resource Acquisition Is Initialization idiom commonly used in C++. It allows the safe use and clean up of all sorts of resources, for example it can be used to always ensure that database connections are closed or locks are always released like below.

mylock = threading.Lock()
with mylock:
    pass # do some thread safe stuff
like image 158
Tendayi Mawushe Avatar answered Oct 24 '22 18:10

Tendayi Mawushe


Actually, the file will be closed when it is garbage collected. See this question for more on how that works.

It is still recommended that you use a try/finally block or a with statement though. If there is an exception when using one of the file object's methods, a reference will be stored in the traceback (which is stored as a global variable) until you clear it or another exception occurs.

Thus, it's bad to rely on garbage collection to close your file for you.

Also, if you've written to the file, you can't guarantee that the changes will be saved to the file until it is closed or flushed.

like image 29
Jason Baker Avatar answered Oct 24 '22 18:10

Jason Baker


Strange that for all the discussion in this topic of the importance of freeing system resources, nobody has mentioned what seems to me an obviously more significant reason to close a file deterministically: so that it can be opened again.

There are certainly cases where it doesn't matter. If a file object goes out of scope or gets deleted, the underlying file will get closed. (When it gets closed depends on the specific implementation of Python you're using.) That's generally going to be good enough - if you know exactly when the file variable is going to go out of scope, and if you know that you don't care if the file gets closed deterministically.

But why should you even be troubling yourself with that kind of analysis when the with statement exists?

like image 13
Robert Rossney Avatar answered Oct 24 '22 17:10

Robert Rossney