[Tutor] Sys.argv read parameters

Mark Lawrence breamoreboy at yahoo.co.uk
Thu Apr 18 00:27:29 CEST 2013


On 17/04/2013 20:27, Danilo Chilene wrote:
> Dear Python Tutor,
>
> I have the code below(file.py):
>
> import sys
>
> a = 'This is A'
> b = 'This is B'
> c = 'This is C'
>
> for i in sys.argv[1]:
>      if sys.argv[1] == 'a':
>          print a
>      if sys.argv[1] == 'b':
>          print b
>      if sys.argv[1] == 'c':
>          print c
>
> I run python file.py a and returns the var a, so far so good.
>
> The problem is that i have a bunch of vars(like a to z), how I can
> handle this in a pythonic way?
>
> Best Regards,
>
> Danilo
>

Further to earlier answers you might like to look at these modules for 
parsing items from sys.argv.

http://docs.python.org/3/library/argparse.html#module-argparse
http://docs.python.org/3/library/getopt.html#module-getopt

http://docs.python.org/3/library/optparse.html#module-optparse is 
available in Python 2.x but is deprecated as of Python 3.2.

There is also an awesome third party module here 
https://github.com/docopt/docopt

-- 
If you're using GoogleCrap™ please read this 
http://wiki.python.org/moin/GoogleGroupsPython.

Mark Lawrence



More information about the Tutor mailing list