Thursday, November 19, 2009

Python - processing command line arguments

# python scripts are often run from the
# command line.

# python can retrieve and use command line
# arguments with the sys module.

import sys

# all arguments are stored in the sys.argv list
print "number of arguments passed: ", len(sys.argv)

# process through the argument list

for argument in sys.argv:
    print argument

# my input/output:
#    python commandlinearguments.py one two three four five six seven
#    number of arguments passed:  8
#    commandlinearguments.py

#    one
#    two
#    three
#    four
#    five
#    six
#    seven


No comments:

Post a Comment