Python ファイルIO

Python3 サンプル

以下のコードは、open、read、write を含む Python の基本的なファイル操作を示しています:

Python with キーワードのリファレンス:https://www.example.com/python3/python3-with-keyword.html

サンプル(Python 3.0+)

# Filename : test.py # author by : www.example.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)

上記のコードを実行した出力結果は次のとおりです:

このテキストはファイルに書き込まれます

Python3 サンプル

その他の拡張