次のコードは、open,read,writeなど、Pythonの基本的なファイル操作を示します。
実例(Python 3.0+)
# Filename : test.py
# author by : www.ceodata.com
# ファイルを書き込む
with open("test.txt", "wt") as out_file:
out_file.write("テキストをファイルに書き込む\ nこんにちは!")
# Read a file
with open("test.txt", "rt") as in_file:
text = in_file.read()
print(text)
上記のコードを実行した結果は次のとおりです。
テキストをファイルに書き込む
こんにちは!
コメントを残す