LoginSignup
3
6

More than 5 years have passed since last update.

Pythonの基礎知識:ファイル書込み編

Last updated at Posted at 2017-11-21

Pythonで簡単なメモ帳を作って見た

Pythonを数時間触ってみて、簡単なシステムを作るという機会があったので、その時に役に立った知識をメモ程度に書いていきます。

1.ファイルへの書き込み

readWrite.py
fout = open('data.txt', 'w')
fout.write('任意の文字列')
fout.close()

まず、
fout = open('data.txt', 'w')
で、data.txtファイルをw:書き込みモードで開きます。

次にファイルを開いて格納した変数foutに書き込みます。
fout.write('任意の文字列')
fout.に対する関数でwrite(str)で指定されたファイルに書き込みます。

最後に、開いているファイルを閉じます。
fout.close()
close()関数で、開いているfoutを閉じます。

そして、テキストファイルdata.txtを開いてみると、任意の文字列が書き込まれていることがわかると思います。

3
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
6