The following is an example on how to pass and access command line arguments which a Python script. Save the following python script to a file eg. python-arguments.py
from sys import argv name, first, second, third, fourth = argv print "Script name is:", name print "Your first argument is:", first print "Your second argument is:", second print "Your third argument is:", third print "Your fourth argument is:", fourth # Alternatively we can access "argv" argument list directly using range. For exmaple: # Print all arguments except script name print argv[1:] # Print second argument print argv[2] # Print second and third argument print argv[2:4] # Print last argument print argv[-1]
The above script will produce a following output if four command line arguments are supplied upon its execution:
$ python python-arguments.py one two three four Script name is: python-arguments.py Your first argument is: one Your second argument is: two Your third argument is: three Your fourth argument is: four ['one', 'two', 'three', 'four'] two ['two', 'three'] four