sys module is also where Python makes available the words typed on the
command that is used to start a Python script. These words are usually
referred to as command-line arguments and show up in
sys.argv, a built-in
list of strings. C programmers may notice its similarity to the C
argv array (an array of C strings).
It’s not much to look at interactively, because no command-line
arguments are passed to start up Python in this mode:
To really see what arguments are about, we need to run a script
from the shell command line. Example 3-1 shows an unreasonably
simple one that just prints the
argv list for inspection.
Example 3-1. PP4E\System\testargv.py
import sys print(sys.argv)
Running this script prints the command-line arguments list; note that the first item is always the name of the executed Python script file itself, no matter how the script was started (see Executable Scripts on Unix).
python testargv.py['testargv.py'] C:\...\PP4E\System>
python testargv.py spam eggs cheese['testargv.py', 'spam', 'eggs', 'cheese'] C:\...\PP4E\System>
python testargv.py -i data.txt -o results.txt['testargv.py', '-i', 'data.txt', '-o', 'results.txt']
The last command here illustrates a common convention. Much like
function arguments, command-line options are sometimes passed by
position and sometimes by name using a “-name value” word pair. For
instance, the pair
option’s value is
data.txt (e.g., ...