Python で with ステートメントを使用してファイルを開く
ファイル オブジェクトを扱うときは、with キーワードを使用することをお勧めします。これには、途中で例外が発生した場合でも、スイートの終了後にファイルが適切に閉じられるという利点があります。また、同等の try-finally ブロックを記述するよりもはるかに短くなります。
In [1]: with open('/tmp/test','r') as f1:
...: print f1.read()
...:
hello, python
In [2]: f1.closed
Out[2]: True
In [3]: f2 = open('/tmp/test', 'r')
In [4]: print f2.read()
hello, python
In [5]: f2.closed
Out[5]: False
Also see PEP 343 -- The "with" Statement