{Python}UTF8ファイルのBase64エンコード

 

https://pythonmaniac.com/file-rw-newline-code/#index_id3


import base64

with open('test2.txt', 'w', encoding='utf-8', newline='') as f:
  f.write('テストファイルです1\r\n')
  f.write('テストファイルです2\r\n')


with open('test2.txt', "r", encoding='utf-8', newline='') as f:
  print( f.read() )

with open('test2.txt', "r", encoding='utf-8', newline='') as f1:
  data = base64.b64encode( f1.read().encode('utf-8') )
  print( data.decode('utf-8') )
  with open('buf.txt', 'w', encoding='utf-8', newline='') as f2:
    f2.write( data.decode('utf-8') )
    
  print( base64.b64decode(data).decode('utf-8') )
  with open('test3.txt', 'w', encoding='utf-8', newline='') as f3:
    f3.write( base64.b64decode(data).decode('utf-8'))