SaltyCrane Blog — Notes on JavaScript and web development

How to pass command line arguments to your Python program

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

Comments


#1 dayo commented on :

Thanks..


#2 bird commented on :

Works, thanks!


#3 JQ commented on :

Thanks!