← Back to course
Code Crew G6+ / Week 08 / Wednesday
3/6
Week 08 Β· Files

Wednesday

Adding to a file
// Saving work that lasts
⏱ about 30 min

Wednesday: Adding to a file

Rocket built a notes program that saves one note to notes.txt. On Monday he saved a note about the fuel tank. On Tuesday he saved a note about the clock.

On Wednesday he opens notes.txt and finds only the clock note. "Where did Monday go?" he asks. "I saved it. I know I saved it."

Raven reads his code and points at one letter. "What do you notice?" she asks. "You open the file with w every time. We learned that w starts the file fresh."

Nova shows two pictures: a page being replaced, and a line being added to the bottom of a page. "Would you like a hint?" she asks. "There is a mode for adding. It is the letter a."

Rocket changes one letter and runs the program twice to be sure.

Mode "a" appends

Mode "a" means append: add to the end. Everything already in the file stays. If the file does not exist yet, Python makes it.

Use "w" when you want to replace a file completely. Use "a" when you want to keep a growing record.

ModeNameIf the file existsIf it does not exist
"r"readreads itFileNotFoundError
"w"writeempties it firstcreates it
"a"appendadds to the endcreates it
# add_note.py
note = input("Note to add: ")
with open("notes.txt", "a") as f:
    f.write(note + "\n")
print("Note saved.")

print("Notes so far:")
with open("notes.txt") as f:
    for line in f:
        print("*", line.strip())

The first run:

Note to add: Fuel tank checked
Note saved.
Notes so far:
* Fuel tank checked

The second run, the next day. The first note is still there:

Note to add: Clock tested
Note saved.
Notes so far:
* Fuel tank checked
* Clock tested
You run add_note.py a third time and add one note. How many notes are listed?
Which mode letter would wipe out the old notes?
WHICH MODE?
  • Read the question.
  • Tap your answer.
A program saves today's high score, replacing the old one. Which mode?
A program keeps a diary, one line per day. Which mode?
At your computer
1. Type add_note.py, save it and run it. Add the note Fuel tank checked.
2. Run it again and add Clock tested. Check that both notes are listed.
3. Change "a" to "w" in the first open. Run it and add a third note. What happened?
4. Change it back to "a". Run it two more times.

Practice: count the visits

This program appends one line each time it runs, then counts the lines. The number grows by one with every run.

# visits.py
with open("visits.txt", "a") as f:
    f.write("visit\n")

count = 0
with open("visits.txt") as f:
    for line in f:
        count = count + 1
print("Visits so far:", count)
Visits so far: 3
TRACE VISITS.PY
  • Read the question.
  • Tap your answer.
What does the first run ever show?
You change "a" to "w". What does every run show?
Why does the second with block use no mode?
After five runs, what number does it show?
Which mode letter makes the file keep growing?
You delete visits.txt, then run once. What number shows?
ModeDoes it keep the old lines?
"a"?
"w"?
"r" (it only reads)?
WHY THIS EXERCISE"w" wipes the file before writing, while "a" adds to the end and "r" only reads.

Append mode turns a file into a growing record. Tomorrow you will build a dated log, a big step toward Mission Log.

← Tuesday