arguments from the prompt

Joshua Macy amused at webamused.com
Tue Feb 8 01:13:47 EST 2000


If you import arv via from sys import argv, then the name in your module
becomes argv.  You don't need to qualify it with sys.

test.py
=======
from sys import argv
print "argv[0] is", argv[0]

import sys
print "argv[0] is", sys.argv[0]


$ python test.py
argv[0] is test.py
argv[0] is test.py


Joshua

joe at localhost.localdomain wrote:
> 
> On Sun, 06 Feb 2000 22:57:29 GMT, Fredrik Lundh <effbot at telia.com> wrote:
> >joe at localhost.localdomain wrote:
> >> how do I write a python script to recive arguments from the
> >> command prompt?
> >
> >you import the sys module, and read up on
> >sys.argv in the manual?
> >
> >(hint: sys.argv contains a list of all command
> >line arguments passed to the program. argv[0]
> >is the name of the program itself).
> >
> >if you want to parse options in a standard
> >fashion, you can use the getopt module.  see
> >the docs for details.
> >
> ></F>
> >
> it says NameError: sys
> Here is the program I am writing (to cheat on my algebra homework when
> they want me to make tables of quadratic functions.)
> I want to be able to type:
> ./prog a b c min max inc
> and have it print a pretty table
> 
> #!/usr/bin/python
> from sys import argv
> 
> a=sys.argv[1]
> b=sys.argv[2]
> c=sys.argv[3]
> start=sys.argv[4]
> end=sys.argv[5]


> inc=sys.argv[6]
> 
> x=start
> for x in range(start,end,inc):
>   y=a*x**2+b*x+c
>   print x,": ",y



More information about the Python-list mailing list