with文は例外発生したら確実に閉じてくれるので便利ですよね。
with文は例外発生したら確実に閉じてくれるので便利ですよね。
# Python3.x(3.1以上) Python2.x(2.7)with open(load.txt) as loadfile, open(save.txt) as savefile:#ここに処理を書く# Python3.x(3.0) とか Python2.x(2.6以下)with open(load.txt) as loadfile:with open(save.txt) as savefile:#ここに処理を書く#close()を持たないurlopenの戻り値なんかを閉じる場合import contextlibimport urllib2with contextlib.closing(urllib2.urlopen('http://www.python.org')) as fp:#ここに処理を書く