Execute a Python script with arguments

  • by

The script – args.py

#!/usr/bin/python
import sys

index = 0

print('Number of arguments: ' + str(len(sys.argv)) + ' arguments.')

for argument in sys.argv:
    print("sys.argv[" + str(index) + "]: " + str(argument))
    index += 1

Executing

python args.py working with arguments

Result

Number of arguments: 4 arguments.
sys.argv[0]: args.py
sys.argv[1]: working
sys.argv[2]: with
sys.argv[3]: arguments

The first argument is always the script name.

Tags: