Friday 18 January 2008

python arguments

What if you want to supply arguments to the Python script? The sys module contains a variable called argv. It is an array that contains the name of the Python file and any command line arguments that followed.

For example, let's define a file called show_args.py;

import sys
print sys.argv

Now when we evaluate show_args.py with Python, we'll simply see the arguments we entered on the command line, along with the filename of the script:

% python show_args.py 1 2 3 4 5
['show_args.py', '1', '2', '3', '4', '5']
%

Notice that sys.argv is an array, so you can refer to individual commands using the [] array element syntax. You can also use any array function on sys.argv or on a part of it (using the [:] syntax).

Here's file show_args_2.py that extracts elements from the sys.argv array:

import sys, string

print 'The arguments of %s are "%s"' % \
(sys.argv[0], string.join(sys.argv[1:]))

(The "\" character lets me continue the print command to the next line by nullifying the "newline" character that would otherwise create a new line.) We'll run this script with the same command-line arguments we used for show_args.py:

% python show_args_2.py 1 2 3 4 5
The arguments of show_args_2.py are "1 2 3 4 5"
%

The sys.argv array consists of strings, so you will need to convert number arguments to numbers using the conversion functions int or float. For example, let's make our pi multiplying script take an argument. We'll call it pi_mult.py:

import math, sys

def times_pi(value):
return math.pi * value

value = float(sys.argv[1])

print '%g times pi is %g' % (value, times_pi(value))

Now when we run it with a command-line argument, that argument is changed into a float before it is multiplied by math.pi:

% python pi_mult.py 2
2 times pi is 6.28319
%

But what if we forget to enter an argument on the command line? We'll get an error message (since there is no second element to the sys.argv array) and Python will stop evaluating the script file:

% python pi_mult.py
Traceback (most recent call last):
File ``pi_mult.py'', line 6, in ?
value = float(sys.argv[1])
IndexError: list index out of range
%

By convention, Unix commands will provide a ``usage'' message if the arguments are wrong. The usage message lists descriptions of the arguments (enclosed in "<" and ">" characters) so you know what kind of arguments the command requires. We can add a check for the right number of arguments to our command, and print out the usage message if the argument count is incorrect.

We'll make a new version, called pi_mult_2.py, in which we add the argument check and the usage message:

import math, sys

if len(sys.argv) != 2:
print 'Usage: pi_mult_2.py '
sys.exit(1)

def times_pi(value):
return math.pi * value

value = float(sys.argv[1])

print '%g times pi is %g' % (value, times_pi(value))

Now when we try to run pi_mult_2.py without arguments, the number of command line arguments is wrong; it should be 2: one for the script filename and one for the number to be multipled by pi. The usage message will be printed instead of causing a Python error:

% python pi_mult_2.py
Usage: pi_mult_2.py
%

No comments:

My photo
London, United Kingdom
twitter.com/zhengxin

Facebook & Twitter