[Tutor] passing variable to python script

bob bgailer at alum.rpi.edu
Fri Oct 14 01:28:50 CEST 2005


At 04:09 PM 10/13/2005, Marc Buehler wrote:
>hi.
>
>i want to pass an argument (a number) to a python
>script when running it:
> > python script.py <number>
>
>i want to be able to use <number> within script.py
>as a parameter.
>
>how do i set this up?

In the sys module there is a property argv. The docs say: "The list of 
command line arguments passed to a Python script. argv[0] is the script name"

import sys
if len(argv) < 2:
   print "Usage: script.py <number>"
   sys.exit(0)
try:
   numeric_arg = int(argv[1])
except:
   print '1st argument must be numeric."
   sys.exit(0)



More information about the Tutor mailing list