Pythonでファイルを書く

f = open('file.txt', 'w', encoding='UTF-8')
f.write('パイソン')
f.close()

f = open('file.txt', 'w', encoding='UTF-8')
f.write('パイソンコーディングを学びます。')
f.close()

f = open('file.txt', 'a', encoding='UTF-8')
f.write('\\\\n')
f.write('Pythonでファイルを読み書きできます。')
f.close()

with open('file.txt', 'w', encoding='UTF-8') as f:
    f.write('パイソンコーディングを学びます。\\\\n')
    f.write('Pythonでファイルを読み書きできます。')

英単語と意味を入力するプログラムを作成する

while True:
    word = input('英単語 : ')
    meaning = input('その意味は何ですか? : ')
    with open('word.txt', 'a', encoding='UTF-8') as f:
        f.write(word)
        f.write(':') # :で英単語と意味を区別します。 apple:リンゴ
        f.write(meaning)
        f.write('\\\\n')
    your_input = input('やめたいならqキーを、 そうじゃなければエンターキーを押してください: ')
    if your_input == 'q':
        break
    else:
        continue

print('単語を入力しました。')

ファイルの書き込み

with open('word.txt', 'r', encoding='UTF-8') as f:
    while True:
        line = f.readline()
        if not line: # これ以上読む内容がなければ
            break
        print(line)

with open('word.txt', 'r', encoding='UTF-8') as f:
    lines = f.readlines()
    print(lines)