Saltycrane logo

SaltyCrane Blog

Notes on Python, Django, and web development on Ubuntu Linux

    

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

3 Comments — feed icon Comments feed for this post


#1 dayo commented on 2008-09-12:

Thanks..


#2 bird commented on 2013-04-19:

Works, thanks!


#3 JQ commented on 2013-05-30:

Thanks!

Post a comment

Required
Required, but not displayed
Optional

Format using Markdown. (No HTML.)
  • Code blocks: prefix each line by at least 4 spaces or 1 tab (and a blank line before and after)
  • Code span: surround with backticks
  • Blockquotes: prefix lines to be quoted with >
  • Links: <URL>
  • Links w/ description: [description](URL)
Created with Django | Hosted by Linode