本記事では、Pythonでのファイルの作成と読み込むサンプルコードを紹介します。
ファイルの作成と読み込み
本記事ではPyhton実行環境として Google Colaboratory を使用します。
test.txt を作成する
下記コードで、test.txt を作成する事ができます。
# ファイルパス
_filepath = './test.txt'
# ファイルに書き込む内容
filecontents = """
1 1月
2 2月
3 3月
4 4月
5 5月
"""
# 書き込みモード
with open(_filepath, 'w') as f:
# ファイル作成、書き込み
f.write(filecontents)
上記コードを実行することで Colab の 「/content」ディレクトリに test.txt が作成されます。

test.txt を読み込む
下記コードで、test.txt を読み込む事ができます。
# ファイルパス
_filepath = './test.txt'
# 読み込みモード
with open(_filepath, 'r') as f:
print(f.read())

まとめ
本記事では、 本記事では、Google Colaboratory でファイルを読み書きする方法を紹介しました。ご参考になりましたら twitter をフォローして SNS でシェアして頂ければ幸いです。