Reading a file is half the contract. The other half is writing. The shape mirrors yesterday — open, do the work, close — but the mode changes:
f = open("out.txt", "w")
f.write("hello\n")
f.close()"w" for write. And .write() is the writing equivalent of .read()?
Yes — but .write() is one-way. It takes a string and pushes it into the file. It doesn't add a newline; if you want lines, you include \n in the string.
What happens if out.txt already exists?
Mode "w" truncates — opens the file at length zero, throwing away whatever was there. If you want to keep the existing content and add to the end, use mode "a" (append) instead.
And if I call .write() multiple times?
Each call appends to whatever you wrote before, in the same open session. Three .write("x\n") calls produces a file with three lines x, x, x. The mode controls what happens at open; subsequent writes always append within the open file.
open(..., "w") then .write(string)f = open("out.txt", "w")
f.write("alpha\n")
f.write("beta\n")
f.close()"w" truncatesMode "w" wipes the file at open time. Whatever was there is gone the moment open("out.txt", "w") returns. Subsequent .write calls grow the file from empty.
"a" appendsf = open("out.txt", "a")
f.write("gamma\n")
f.close()Mode "a" opens at the end of the file. Existing content stays. New .writes extend it.
.write() does NOT add newlinesA classic gotcha — coming from print, you might expect newlines for free. You don't get them:
f.write("alpha")
f.write("beta")
# File contains: alphabeta ← one line, no separatorIf you want lines, write the \n yourself.
.writelines(iterable) — write many at onceGiven an iterable of strings, write each in order. Still no automatic newlines:
f.writelines(["alpha\n", "beta\n", "gamma\n"])For most code, a for loop with .write is just as clear.
| Mode | At open | Where writes go |
|---|---|---|
"r" | nothing — file must exist | (read only) |
"w" | truncates to empty | start of file (now empty) |
"a" | nothing — leaves existing content | end of file |
.close()f = open("out.txt", "w")
f.write("hello")
# (forgot to close)Depending on the OS, the bytes might still be in a buffer when your script exits — meaning the file looks empty if anything else reads it. Tomorrow's with statement makes this impossible to forget.
Reading a file is half the contract. The other half is writing. The shape mirrors yesterday — open, do the work, close — but the mode changes:
f = open("out.txt", "w")
f.write("hello\n")
f.close()"w" for write. And .write() is the writing equivalent of .read()?
Yes — but .write() is one-way. It takes a string and pushes it into the file. It doesn't add a newline; if you want lines, you include \n in the string.
What happens if out.txt already exists?
Mode "w" truncates — opens the file at length zero, throwing away whatever was there. If you want to keep the existing content and add to the end, use mode "a" (append) instead.
And if I call .write() multiple times?
Each call appends to whatever you wrote before, in the same open session. Three .write("x\n") calls produces a file with three lines x, x, x. The mode controls what happens at open; subsequent writes always append within the open file.
open(..., "w") then .write(string)f = open("out.txt", "w")
f.write("alpha\n")
f.write("beta\n")
f.close()"w" truncatesMode "w" wipes the file at open time. Whatever was there is gone the moment open("out.txt", "w") returns. Subsequent .write calls grow the file from empty.
"a" appendsf = open("out.txt", "a")
f.write("gamma\n")
f.close()Mode "a" opens at the end of the file. Existing content stays. New .writes extend it.
.write() does NOT add newlinesA classic gotcha — coming from print, you might expect newlines for free. You don't get them:
f.write("alpha")
f.write("beta")
# File contains: alphabeta ← one line, no separatorIf you want lines, write the \n yourself.
.writelines(iterable) — write many at onceGiven an iterable of strings, write each in order. Still no automatic newlines:
f.writelines(["alpha\n", "beta\n", "gamma\n"])For most code, a for loop with .write is just as clear.
| Mode | At open | Where writes go |
|---|---|---|
"r" | nothing — file must exist | (read only) |
"w" | truncates to empty | start of file (now empty) |
"a" | nothing — leaves existing content | end of file |
.close()f = open("out.txt", "w")
f.write("hello")
# (forgot to close)Depending on the OS, the bytes might still be in a buffer when your script exits — meaning the file looks empty if anything else reads it. Tomorrow's with statement makes this impossible to forget.
Create a free account to get started. Paid plans unlock all tracks.