Python Forum

Full Version: python with arguments
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
here's an example of what i would like to do

    def infos():
        print "His name is John, he is 24"
        print "His name is Brad, he is 17
        print "His name is Marc, he is 41
        print "His name is Leo, he is 51
i would like that when i run my script like this:
" python names.py -j ", it would only print :
Output:
His name is John,he is 24
" python names.py -m " , it would onl print :
Output:
His name is Marc, he is 41
etc..
how would i do that?
thanks
This will give you a good start.

#!/usr/bin/env python

# reading arguments given on the command line when script invoked
# (if invoked with nothing after script name, will generate a failure)
import sys     # import module that provides command line arguments
x = sys.argv   # assign to x a list of the command line arguments
print(x[1])    # print the 2nd argument (first will be name of involved script)