概要
Pythonでなにかしらを書いているときに、よく調べたりしていたちょっとした処理をまとめます。
ファイル処理
ファイルの読み込み、書き込みについてまとめます。
ファイル書き込み
# ファイルに書き込む文字列
s = 'test string'
# ファイル名
path = 'test_write.txt'
# ファイルに書き込む処理
with open(path, mode='w') as f:
f.write(s)
ファイル読み込み
# ファイル名
path = 'test_write.txt'
# ファイルから読み込む処理
with open(path) as f:
s_read = f.read()
# 読み込んだ内容を表示
print(s_read)