Command Line Arguments-

  • – Python application can also be run by using CMD.
  • – We can also pass arguments during python program execution via CMD.
  • – All the argument which are passed via CMD will be stored into a pre-defined class object named as “argv”.
  • – It belongs to a module named as “sys”
  • – By default all the data passed from CMD will treated as a string type.
  • – Whatever data we will pas after the command “python” will be treated as the arguments of “argv”
  • – Class name will always be stored at 0th index of object argv and can be printed as argv[0]

Ex-

import sys
class TestCmd:
    def main():
        file_name = sys.argv[0] #pre-defined class object named as "argv".
        argument1 = sys.argv[1]
        print("File Name-", file_name)
        print("Argument1-", argument1)
        print("Argument2-", sys.argv[2])

        length = len(sys.argv)
        print("Total no. of arguments-", length)
TestCmd.main()

Command Used To Execute The Program-

CMD>python Test.py Python 27.2 abc
Output-
	File Name- Test.py
	Argument1- Python
	Argument2- 27.2
	Total no. of arguments- 4
Menu