open、close
with、open。(closeは不要)
プログラム(その1)
f = open('test.txt', 'w')
for i in range(0,100):
f.write(str(i) + '\n')
f.close()
実行結果
(test.txtの中身)
0
1
(省略)
98
99
プログラム(その2)
with open('test_b.txt', 'w') as f:
for i in range(0,100):
f.write(str(i) + '\n')
実行結果
その1と同じ
参考資料
「Python ゼロからはじめるプログラミング」サポートページ (tsukuba.ac.jp)
ゼロから始めるPython(7) - Qiita
例外処理 クリーンアップ動作の定義- Python3エンジニア認定基礎試験 #36 - YouTube