๐Ÿ“– Beginning Python Programming /
Module: Files Operation

Code Example: Diary Text File

  1. Diary v1 60%
  2. Diary v2 60%

๐Ÿ“– Beginning Python Programming / Files Operation / Code Example: Diary Text File

Diary v1

import datetime

content = input("What do you want to say to Mr. Diary? ")
if len(content) > 0:
    with open('diary.txt', "a") as file_obj:
        today = str(datetime.date.today())
        file_obj.write(today + ": " + content + "\n")

with open('diary.txt', "r") as file_obj:
    lines = file_obj.readlines()
    print("".join(lines[-3:]).rstrip())