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:

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.


import sys

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

if __name__ == "__main__":

Try it out:
$ python arg1 arg2 arg3

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

No comments:


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

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.