Passing argument from Command-line in Python

Sometimes it’s required to use arguments passed from cli to use in our program. Its very simple to do so..

we just need to use sys module’s argv method.

import sys

print(sys.argv[1])
print(sys.argv[2])

Note that using sys.argv[0] will just give program.py, which is in fact the first argument. 

Leave a Reply

Your email address will not be published.