Hello
@colin ,
it looks like you are running the file incorrectly as you are not specifying the command in terminal.
I do not have the Python3 book only the Python2, I had a try at this one though to help

.
The Python, notice the line changes and indentations.

The test.txt file used (contents are his words not mine) from an exercise in the Python2 book, I just renamed it to use for this try.

How to run it in terminal

On highlight in terminal see #1, #2 and #3 above, these are the components of the command that you need.
For #2 you can either cd to the directory containing your Python files, or you can enter in your Path as previously. I just put it in Home directory as its temporary for this post.
#3 is what you have missed in the terminal command ? or it is in another folder than the Python file is.