Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Command line arguments

  • sys
  • argv
import sys

def main():
    print(sys.argv)
    print(sys.argv[0])
    print(sys.argv[1])
    print(sys.argv[2])

main()
$ python examples/basic/cli.py one two
['examples/basics/cli.py', 'one', 'two']
examples/basics/cli.py
one
two
$ python examples/basic/cli.py
['examples/basics/cli.py']
examples/basics/cli.py
Traceback (most recent call last):
  File "examples/basics/cli.py", line 6, in <module>
    print(sys.argv[1])
IndexError: list index out of range