python 'with' statement, should I use contextlib.closing?

Viewed 14046
from contextlib import closing

def init_db():
    with closing(connect_db()) as db:
        with app.open_resource('schema.sql') as f:
            db.cursor().executescript(f.read())
        db.commit()

This is from flask tutorial Step 3(http://flask.pocoo.org/docs/tutorial/dbinit/#tutorial-dbinit). And I'm little curious about the line 4 of that.

Must I import and use that 'contextlib.closing()' method?

When I've learned about with statement, many articles said that it closes file automatically after process like below.(same as Finally: thing.close())

with open('filename','w') as f:
    f.write(someString);

Even though I don't use that contextlib.closing() like below, What's difference? It's from version 2.7.6, Thank you.

def init_db():
    with connect_db() as db:
        with app.open_resource('schema.sql') as f:
            db.cursor().executescript(f.read())
        db.commit()
2 Answers
Related