Reading and writing files

INPUT · Slides

Writing out to a file

01 / 07

Pass a mode when you write

Every open so far has only read. In fact open takes a second argument called the mode, and leaving it out gave you "r" (read).

When you want to write, pass "w" (write). Once the file is open, write puts characters into it. And with still closes the file for you on the way out — exactly as it did for reading.

with open("memo.txt", "w") as f:    f.write("morning")print("done writing")

Result

done writing

02 / 07

Once written, you can read it back

Whether it really went in is something you find out by opening the file again and reading. The point to notice is that writing and reading are two separate openings.

One more thing worth knowing. If no file of that name exists, "w" creates it. Opening a missing file with "r" stopped you dead with FileNotFoundError. That is the big difference between the two modes.

with open("memo.txt", "w") as f:    f.write("morning")with open("memo.txt") as f:    content = f.read()print(content)

Result

morning

03 / 07

w empties the file first

This is the part of the chapter to be careful about. The moment you open with "w", whatever was already in the file is gone.

In the example below, morning is nowhere to be found. Only night, written afterwards, is left.

Opening a file that mattered with "w" and emptying it by accident is a mistake experienced people still make. Get into the habit of asking, before you open, whether this file is allowed to disappear.

with open("memo.txt", "w") as f:    f.write("morning")with open("memo.txt", "w") as f:    f.write("night")with open("memo.txt") as f:    content = f.read()print(content)

Result

night

04 / 07

You do not get a newline for free

print added a newline at the end of the line for you. write does not. What you write simply carries on from what came before.

So even though this writes twice, reading it back gives you morningnoon stuck together on one line.

with open("memo.txt", "w") as f:    f.write("morning")    f.write("noon")with open("memo.txt") as f:    content = f.read()print(content)

Result

morningnoon

05 / 07

Write the newline yourself

If you want separate lines, write the \n yourself. That alone gives you a file of one thing per line.

Read it back and print it and you get one blank line, as below. The contents of the file end in a newline and print adds another. Nothing is wrong.

with open("memo.txt", "w") as f:    f.write("morning\n")    f.write("noon\n")with open("memo.txt") as f:    content = f.read()print(content)

Result

morning
noon

06 / 07

a adds without wiping

When you want to carry on from what is there rather than wipe it, use "a" (append). Opening leaves the contents alone and puts yours on the end.

Three modes are enough to remember.

  • "r" … read (the one you get when you leave the mode out)
  • "w" … wipe, then write
  • "a" … keep, and add
with open("diary.txt", "w") as f:    f.write("day 1\n")with open("diary.txt", "a") as f:    f.write("day 2\n")with open("diary.txt") as f:    content = f.read()print(content)

Result

day 1
day 2

07 / 07

Write a whole list at once

There is also writelines, which writes the contents of a list in one go. The name has lines in it, but it does not add newlines, so you have to put the \n on the items yourself.

You can now leave characters outside your program. Next we read back what we wrote and make it a round trip.

fruits = ["apple\n", "orange\n"]with open("fruit.txt", "w") as f:    f.writelines(fruits)with open("fruit.txt") as f:    content = f.read()print(content)

Result

apple
orange