How to pass command line arguments to your Python program



This is my OLD blog. I've copied this post over to my NEW blog at:

http://www.saltycrane.com/blog/2007/12/how-to-pass-command-line-arguments-to/

You should be redirected in 2 seconds.



Here is an example for quick reference. argv holds the program name at index 0. That's why we start at 1.

#!/usr/bin/python

import sys

def main():
    # print command line arguments
    for arg in sys.argv[1:]:
        print arg

if __name__ == "__main__":
    main()

Try it out:
$ python cmdline_args.py arg1 arg2 arg3
arg1
arg2
arg3

See also:
sys module documentation
getopt module documentation
Guido van Rossum's post

No comments:

About

This is my *OLD* blog. I've copied all of my posts and comments over to my NEW blog at:

http://www.saltycrane.com/blog/.

Please go there for my updated posts. I will leave this blog up for a short time, but eventually plan to delete it. Thanks for reading.