from pathlib import Path notes = Path("notes.txt") notes.write_text("buy milk\ncall grandma\nfinish lesson 7\n") with notes.open() as f: for number, line in enumerate(f, start=1): print(number, line.strip()) # append instead of overwriting with notes.open("a") as f: f.write("water the plants\n") print(notes.read_text().count("\n"), "lines now")