Back to Contents
We have been building progressively larger and larger programs, but they have all been run from Python’s interactive interpreter. Now, we shall write stand-alone programs, to be invoked at the command line. This means we can use them just like any other program on our computer, or share them with friends.
We wish to build stand-alone programs which we can run directly from the command line. The
sys module provides the list
sys.argv which contains, first, the name of the running script, and then any other arguments provided when the script was run. For example, consider the following program, saved as
We can run it and see what happens:
$ python standalone.py This program is called standalone.py There are 0 command line arguments $ python standalone.py a b c This program is called standalone.py There are 3 command line arguments Argument 0 is a Argument 1 is b Argument 2 is c
Remember that on some systems, you might need to type
python3 instead of
$ is the command line prompt on the author’s computer – it may be different on yours.
Now we can write stand-alone programs, to which we provide filenames and other arguments, instead of putting those details directly in the Python program itself. Much more flexible!
We shall now write a stand-alone version of out text statistics program from chapter 9. It will take the filename as an argument. In addition, we shall split our program into two: a file
textstat.py to contain the bulk of the program, and another
textstats.py to contain the part to do with command line arguments. Here is
Now, we can write the main program
textstats.py, which will use the
import keyword to access the
stats_from_filename function of the
The purpose of splitting the program this way is to allow the function
stats_from_file and the function
stats_from_filename to be used in other contexts without having to alter the whole program. Now we can run the program on its own, without loading an interactive Python session:
$ python textstats.py gregor.txt 8 lines, 472 characters, 85 words, 4 sentences
In the questions, we will make stand-alone versions of some of our other programs, and some entirely new ones.
It is important, just as with any other list, to check that there is as much information as we expect in
sys.argv, before looking up elements in it, or slicing it. If not, we can print out an error message, and a description of correct usage for the user. For example:
We have gone all the way from introducing addition in chapter 1, to building stand-alone programs in this chapter. We now have the tools to tackle larger projects, and that is what we shall be doing in the next four chapters.
In question 3 of chapter 11 we updated our number-guessing game. Make a stand-alone program from this. It should take one argument, which is the maximum number. If no number is given, 100 is used as a default.
In question 5 of chapter 10 we wrote a function to plot a graph of a given function. Write a self-contained command line program to plot any function given as an argument, over a range similarly given. The built-in Python function
eval can evaluate a given piece of Python program. For example, if the variable
x has value
10 the result of
eval(’x * 2’) is
20. Be sure to split your program into two modules: one to deal with the command line argument and one to do the graph plotting. Handle errors appropriately.
Write a simple note-taking program. When we run
python note.py add todo "mow the lawn" the note
mow the lawn should be added to the end of the file
todo.txt. If the file does not exist, it should be created. Now extend the program to allow
python note.py list which will list the notes by number. Running
python note.py remove 4 should remove task number 4.