【Python】 Python ~ 基本編 / ファイル読込・書込 ~

■ はじめに

Python の ファイルの扱い方を学ぶ

目次

【1】読み込み
 1)read()
 2)readlines()
【2】書き込み
 1)write()
  [補足] 書き込みモード
【3】with ブロック
【4】日本語(UTF-8)対応
【5】サンプル
 例1:read() / with
 例2:readlines() / with
 例3:write() / with
 例4:UTF-8 (codecs) の write / read

【1】読み込み

1)read()

ファイル全体を文字列として読み込む

2)readlines()

 1行ごとに読み込む

【2】書き込み

1)write()

文字列を書き込み

[補足] 書き込みモード

1)mode='w’:上書きで書き込む()
2)mode='a’:追加で書き込む

【3】with ブロック

* 終了時に自動的にクローズされる
 => C# の using , java の try-with-resources みたいなもん

with open('hello.txt') as file:
   text = file.read()

補足:Context Managers(コンテキストマネージャ)

with 文に関連して、コンテキストマネージャってのがある。
長くなったので、以下の関連記事を参照のこと

Python ~ 基本編 / クラス・継承 ~
https://dk521123.hatenablog.com/entry/2019/08/29/220537

【4】日本語(UTF-8)対応

codecs を使う

import codecs

with codecs.open('japanese.txt', 'r', 'utf-8') as file:
   text = file.read()

【5】サンプル

例1:read() / with

def main():
    with open('hello.txt') as file:
        text = file.read()
        print(text)

if __name__ == '__main__':
    main()

hello.txt

Hello
World!!
Tom

出力結果

Hello
World!!
Tom

例2:readlines() / with

with open('hello.txt', 'r', encoding='utf-8') as file:
  line_number = 1
  lines = file.readline()
  while lines:
    print(f'{line_number} - {lines}')
    if ('Tom' in lines):
      print("Hit name!")
      with open('hello_out.txt', mode='a') as out_file:
        out_file.write(lines)

    line_number = line_number + 1
    lines = file.readline()

出力結果

1 - Hello

2 - World!!

3 - Tom
Hit name!

例3:write() / with

def main():
    hello = 'hello world, Mike!!'
    with open('hello_out.txt', mode='w') as file:
        file.write(hello)

if __name__ == '__main__':
    main()

例4:UTF-8 (codecs) の write / read

import codecs

def main():
  hello = 'こんにちは、世界!'
  with codecs.open('japanese.txt', 'w', 'utf-8') as file:
    file.write(hello)

  with codecs.open('japanese.txt', 'r', 'utf-8') as file:
    text = file.read()
    print('Result : ' + text)

if __name__ == '__main__':
    main()

出力結果

Result : こんにちは、世界!

参考文献

https://yukun.info/python-file/
withブロック
https://note.nkmk.me/python-file-io-open-with/
codecs
https://qiita.com/kanemu/items/1080972679c9cb70ebff

関連記事

Python ~ 基本編 / 文字列 ~
https://dk521123.hatenablog.com/entry/2019/10/12/075251
Python ~ 基本編 / JSON
https://dk521123.hatenablog.com/entry/2019/10/12/075251
Python ~ 基本編 / クラス・継承 ~
https://dk521123.hatenablog.com/entry/2019/08/29/220537
Pythonでの文字コード関連のトラブルシューティング
https://dk521123.hatenablog.com/entry/2020/06/19/121139