The Command Line, Enough of It · practice
Run Your Own Program
This is the lesson the chapter was built for.
python is a program like any other, and the
python report.py
You have read 3 books.
- The Left Hand of Darkness
- Piranesi
- Station Eleven
That is the report.py sitting in your project, the one ls has been showing you since the second lesson. You did not need to copy it into another runner or submit it first. The shell started Python and passed it report.py as an
Where the filename is looked up
report.py is a ls and cat. If you are standing in data and type python report.py, Python will tell you it cannot find the file, because from there, it cannot.
This is one of the most common ways a first script fails to run. It is not a problem inside the Python file.
Sending output somewhere other than the screen
By default a program’s output goes to your terminal. > sends it to a file instead. We will put it in the notes directory you made in the previous lesson:
python report.py > notes/summary.txt
Check with ls that notes is actually there. If it is not, you either skipped the previous lesson or reset the project, and mkdir notes puts it back.
One thing about directories is worth knowing before you run that redirect, because it explains most confusing results at this step.
> creates the file it is pointing at, but never a directory to put it in. Redirect into a directory that does not exist and the shell stops with No such file or directory, naming the path it could not open. That message is about the folder, not your program.
Nothing prints when the redirect works, which is correct: the output went into the file. cat notes/summary.txt will show you it arrived.
It works with any command, not just Python. echo simply prints whatever you give it, which makes it the shortest way to see the redirect on its own:
echo hello > /tmp/pip-redirect-demo.txt
cat /tmp/pip-redirect-demo.txt
Two more things to know about > before you rely on it. It creates the file if it does not exist, and it replaces the contents if it does, without asking. And it is the shell doing this, not Python: your program still just prints, and the shell decides where printing lands. That separation is why the same redirection works with many commands.
Editing a file without leaving the terminal
To change report.py you need an editor, and the Python Land terminal includes one. nano opens a file for editing right where you are standing:
nano report.py
The file fills the screen, and the arrow keys are enough to move around it. Along the bottom nano ^ means the Control key:
Ctrl + Owrites the file out. It asks you to confirm the name; press Enter.Ctrl + Xleavesnanoand gives you back the prompt.
Save, then exit. Those two are the whole of what you need today.
nano is not the editor you will use for real work, and Chapter 4 installs one that is. It is here because editing a file over a terminal is a thing you will need one day on a machine that has nothing else, and because it is the shortest path from “I can look at files” to “I can change them”.
Your turn
Run
ls. If it does not shownotes, runmkdir notes.Open
report.pywithnanoand add"The Dispossessed"to thebookslist. Save withCtrl + O, then exit withCtrl + X.Run
python report.pyand check the output says four books and lists yours.Run it again, sending the output to
notes/summary.txt.cat notes/summary.txtto confirm it arrived.
Then press Check my work. Do that after step 5, not after step 1: until the file exists, there is nothing saved for the check to find.
If step 3 still prints three books, inspect cat report.py: does the saved books list contain all four titles? Check the added title and save with Ctrl + O, then Enter. If the saved file looks right, use pwd to check that you are running the intended project copy. Python reads the source file again on each invocation.
If step 4 says No such file or directory, read which path it names. Bash names notes/summary.txt; a missing notes directory prevents it from creating that file. Go back to step 1, then come straight back to step 4.
You run python report.py > notes/summary.txt and nothing appears on screen. What happened?