SaltyCrane Blog —  Notes on Python 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.


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

3 Comments — feed icon Comments feed for this post

#1 dayo commented on 2008-09-12:


#2 bird commented on 2013-04-19:

Works, thanks!

#3 JQ commented on 2013-05-30:


Post a comment

Required, but not displayed

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 and Bootstrap | Hosted by Linode