Lazo Lab Sign up

Python Fundamentals / Functions and errors

Lesson 36 of 38

Writing files

Open a file with 'w' to write (this replaces the contents) or 'a' to add to the end.

Key points

  • 'w' overwrites everything
  • 'a' appends
  • Add \n for new lines
with open("log.txt", "a") as f:
    f.write("Logged in\n")
Watch a video on thisOpens YouTube search results for “Writing files” in a new tab

Quiz · +10 XP

Which mode adds to the end of a file without erasing it?

Log in to save progress and earn XP.