Sunday, August 25, 2019

Python - Command Line Arguments


Sys.argv deals with the no of arguments that will be passed through the command line arguments.The command line arguments represent all the arguments coming through the command line.The no of command line arguments start with 0.

We will write a sample code check the usefulness of this command.

We need to import the sys module for it.


Let us run the program and check the output.








Output :-








The first print statement gives us the name of the program as we are giving the argument as 0 (sys.argv[0]).

The second print statement will print the total number of arguments passed in the above program.


We should note the below points regarding command line arguments :-

  • The sys.argv takes the command line arguments in the form of a list.
  • The first element in the list is the name of the file.
  • The arguments always come in the form of a string even if we type an integer in the argument list. We need to use int() function to convert the string to integer.

No comments:

Post a Comment

Hadoop - What is a Job in Hadoop ?

In the field of computer science , a job just means a piece of program and the same rule applies to the Hadoop ecosystem as wel...