Rocket gives his favorite planet tracker to a new crew member. She runs it, and it stops at once with FileNotFoundError.
"It works on my computer," Rocket says, confused. Then he realizes why. "Oh. My computer already has the file."
Raven nods. "What do you notice?" she asks. "Every program that loads a file needs a plan for the first run, when there is nothing to load yet."
Nova shows two paths: a file that exists, and an empty starting point. "Would you like a hint?" she asks. "Check whether the file exists first. If it does not, start with empty data. The first save will make the file."
Rocket imports one more module: os.
The os module lets a program ask questions about files. The test os.path.exists("stats.json") is True if that file is in the folder.
The pattern: if the file exists, load it. Otherwise, start with empty data. Either way, save at the end. Next week you will meet another way to do this, with try and except.
# planet_tally.py
import json
import os
FILE = "stats.json"
def load_stats():
"""Load saved stats, or start fresh on the first run."""
if os.path.exists(FILE):
with open(FILE) as f:
return json.load(f)
return {"runs": 0, "favorites": {}}
def save_stats(stats):
with open(FILE, "w") as f:
json.dump(stats, f, indent=2)
stats = load_stats()
stats["runs"] = stats["runs"] + 1
planet = input("Favorite planet today: ").strip().title()
favorites = stats["favorites"]
favorites[planet] = favorites.get(planet, 0) + 1
save_stats(stats)
print(f"Run number {stats['runs']}")
for name, count in favorites.items():
print(f"{name}: {count}") .title() makes the first letter of each word a capital and the rest small, so mars, MARS and Mars all count together. Three runs in a row:
Favorite planet today: Mars Run number 1 Mars: 1
Favorite planet today: venus Run number 2 Mars: 1 Venus: 1
Favorite planet today: MARS Run number 3 Mars: 2 Venus: 1
This short program remembers how many times it has run. On the very first run there is no file, so it starts from zero.
# launch_counter.py
import json
import os
FILE = "launches.json"
if os.path.exists(FILE):
with open(FILE) as f:
data = json.load(f)
else:
data = {"launches": 0}
data["launches"] = data["launches"] + 1
with open(FILE, "w") as f:
json.dump(data, f)
print("Launches so far:", data["launches"]) | Statement | True or false? |
|---|---|
| os.path.exists checks whether a file is there. | ? |
| json.load on a missing file gives a FileNotFoundError. | ? |
| The first run should always fail so you know to make a file. | ? |
| Saving at the end creates the file if it is missing. | ? |
Your programs now handle the first run gracefully. That is the last piece Mission Log needs for saving. Tomorrow is a Crew Quest.