Checkpoint

Checkpoint: The Keeper's Fuel Log

Every lantern keeper writes down each refill in a fuel log. The Guild wants one tool that adds to a log and adds up the totals. Some keepers scribble notes and smudges in their logs, so be careful when reading.

This checkpoint uses the whole chapter: file modes, reading line by line, pathlib and catching errors.

Assignment

Each line of a log looks like island,amount, for example Gull Rock,4.

  1. Complete log_fuel(path, island, amount):
    • Make the folder that will hold path, with any missing parents.
    • Add the line island,amount to the end of the file. Don't erase what is already there!
  2. Complete fuel_totals(path). Return a dict that maps each island to its total amount (an int).
    • Skip blank lines and lines starting with #.
    • Split each line at the first comma, and strip spaces.
    • Skip any line whose amount isn't a whole number.
    • If the file doesn't exist, return {}.