Python Append Mode
Table of Contents + β
In the last lesson you learned about Writing Files. There you opened a file in "w" mode and put text into it. That worked, but it came with one real problem. Every time you opened the file in "w", it wiped out whatever was already inside. So how do you add new text to a file and keep the old text too? That is exactly what append mode is for.
π€ Why Append Mode?
Picture a log file. Every time your program runs, you want to write one new line that says it ran. The old lines should stay. The new line goes at the bottom.
Here is the pain. If you open that file in "w" mode, Python erases everything first. So each run throws away the whole history. You end up with just the latest line and nothing before it. All that record is gone.
The fix is one letter. Open the file in "a" mode instead of "w". The "a" stands for append. It means add to the end. The old content stays untouched. Your new text is written right after it.
π§© What βaβ Mode Does
Think of "a" mode like adding a new message to a group chat. You are not deleting the old messages. You are just adding yours at the bottom. The whole conversation stays, and your line joins the end of it.
Here is the simplest possible use. This opens a file in append mode and adds one line.
with open("log.txt", "a") as file: file.write("Program ran\n")Read it piece by piece.
open("log.txt", "a")opens the file in append mode. If the file does not exist yet, Python creates it for you.withmakes sure the file closes properly once we are done.file.write(...)adds the text. Because we are in"a"mode, this text goes at the very end of the file.- The
\nat the end is a newline. It moves to the next line, so the next thing you append starts fresh below.
There is no output on the screen here. The result lives inside the file. Open log.txt and you will see one line.
Output
Program ranTip
End each appended line with \n. Without it, the next line you append sticks straight onto the end of this one. Then your log becomes one long messy line.
π Watch the File Grow
The whole point of append is that the file grows each run. The old lines stay, and a new one joins the bottom. Let us actually see that happen.
Say you run this same little program three separate times.
with open("log.txt", "a") as file: file.write("Program ran\n")After the first run, the file holds one line.
Output
Program ranAfter the second run, the old line is still there. The new line is added below it.
Output
Program ranProgram ranAnd after the third run, you have three lines. Each run added one. None got erased.
Output
Program ranProgram ranProgram ranSee the file growing? That is "a" mode doing its job. Every run keeps the history and adds to it.
π Append vs Write
This is the part that trips people up, so let us put the two side by side. Both "a" and "w" write to a file. The difference is what happens to the old content.
| Question | βwβ (write) | βaβ (append) |
|---|---|---|
| What happens to old content? | Erased, the file starts empty | Kept, nothing is lost |
| Where does new text go? | At the start of a fresh file | At the end, after the old text |
| Creates the file if missing? | Yes | Yes |
| Good for? | Replacing the whole file | Adding to a log or history |
Let us prove the difference with code. Here is the same file opened twice in "w" mode.
with open("notes.txt", "w") as file: file.write("First line\n")
with open("notes.txt", "w") as file: file.write("Second line\n")You might expect both lines. But "w" erased the file the second time, so only the last write survives.
Output
Second lineNow the exact same thing with "a" mode.
with open("notes.txt", "a") as file: file.write("First line\n")
with open("notes.txt", "a") as file: file.write("Second line\n")This time the first line stayed, and the second joined the end.
Output
First lineSecond lineThat is the whole idea in one picture. "w" replaces, "a" adds.
π A Real Example: A Simple Log
Let us build something you would actually use. Imagine an app like WhatsApp keeping a small record of when someone logs in. Each login should add a fresh line to a log file, with the old lines kept.
We will use "a" mode so the history grows instead of getting wiped.
def log_event(message): with open("activity.log", "a") as file: file.write(message + "\n")
log_event("Alex logged in")log_event("Alex opened settings")log_event("Alex logged out")Walk through it.
log_eventtakes a message and opens the log file in"a"mode.file.write(message + "\n")adds the message at the end, then a newline so the next entry starts on its own line.- We call it three times, so three lines get added in order.
After all three calls, the log file holds the full history.
Output
Alex logged inAlex opened settingsAlex logged outThe nice part is that nothing was erased between calls. Each log_event simply added its line to the bottom. If you run the program again tomorrow, those new lines join below these ones. That is how real log files grow over time.
β οΈ Common Mistakes
A few traps catch people the first time they use append. Watch for these.
- Using
"w"when you meant"a". This is the big one."w"quietly erases the file, so you lose everything you had before.
# β Avoid: "w" wipes the file every run, history is lostwith open("activity.log", "w") as file: file.write("Alex logged in\n")
# β
Good: "a" keeps the old lines and adds the new onewith open("activity.log", "a") as file: file.write("Alex logged in\n")- Forgetting the
\nat the end. Without a newline, your entries run into each other on one line.
# β Avoid: lines stick together as "FirstSecond"file.write("First")file.write("Second")
# β
Good: each entry sits on its own linefile.write("First\n")file.write("Second\n")- Expecting append to read the file.
"a"mode is for writing to the end only. If you try to read from a file opened in"a", Python will not let you. Open it in read mode for that.
β Best Practices
Small habits that keep your append code clean and safe.
- Use
"a"whenever you want to keep the old content and add to it, like logs, records, or history. - Use
"w"only when you truly want to start the file over from empty. - End every appended line with
\nso each entry stays on its own line. - Open the file with
with, so it always closes properly even if something goes wrong.
π§© What Youβve Learned
β
Append mode "a" adds new text to the end of a file without erasing what is already there.
β
"w" mode wipes the file first, so it only keeps the latest write. "a" keeps the full history.
β
Both "a" and "w" create the file if it does not exist yet.
β
Each run in "a" mode makes the file grow, which is exactly how a log file should behave.
β
End every appended line with \n so entries do not run together.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What does opening a file in "a" mode do?
Why: Append mode "a" writes new text at the end of the file while keeping everything that was already there.
- 2
You open the same file in "w" mode and write to it twice. What is left in the file?
Why: "w" mode erases the file every time it is opened, so only the final write survives.
- 3
What happens if you open a file in "a" mode and the file does not exist yet?
Why: Like "w", append mode creates the file if it is missing, then writes to it.
- 4
Why should each appended line end with \n?
Why: The \n is a newline, so the next appended entry starts on a fresh line instead of sticking to the last one.
π Whatβs Next?
You can now grow a file safely without losing what was already there. Next we look at a common file format you will meet everywhere, from spreadsheets to data exports, and how to read and write it in Python.