f = open(filename) d = f.read() # ファイル終端まで全て読んだデータを返す f.close()
明示的にクローズしなくても、これで閉じられる。
with open(path) as f: s = f.read()
行ごとに分割したリストとして読み込む。
with open(path) as f: l = f.readlines()
with open(path) as f: for s_line in f: print(s_line)
with open(filename, mode='w') as f: f.write("データ")
with open(path_w, mode='x') as f: f.write(s)