# test.txtファイルの内容 #atmark IT # #deep insider # ファイルをオープンして、1行ずつその内容を読み込んで処理する with open('test.txt') as f: for line in f: line = line.rstrip() # 読み込んだ行の末尾には改行文字があるので削除 print(line) # 出力結果(4行目に空行が表示されるときとされないときがあるのを除き、以下同じ) #atmark IT # #deep insider # テキストファイルをオープンして、その内容を全て読み込み、クローズする f = open('test.txt') # f = open('test.txt', 'rt'): s = f.read() # ファイルの全内容が1つの文字列として返される print(s) f.close() # wi
![[解決!Python]テキストファイルの読み書きまとめ](https://cdn-ak-scissors.b.st-hatena.com/image/square/29964bcda60b3414b87ec00cd90deb99fb430ce0/height=288;version=1;width=512/https%3A%2F%2Fimage.itmedia.co.jp%2Fait%2Farticles%2F2106%2F15%2Fcover_news016.png)