Python Command Line Arguments

Hello everyone, In this tutorial, we’ll be learning about Python Command-line Arguments and how we can use them in our Codes. We will be using the sys module whose functions provide us the ability to interact with the command line interpreter and because it is an in-built module, we are not required to install it. So let’s get started with a brief introduction of command-line arguments.

What are Command-Line Arguments in Python?

These are the arguments that we give while executing the code to the command line interpreter with the script name. In Python, we use sys.argv which is a list of all command command-line arguments given on the Command-line shell. The first element of the sys.argv list that is, sys.argv[0] is the script name and if no script name was passed to the interpreter, sys.argv[0] returns an empty string. By default, all the elements in this list are of string type. Using command-line arguments have their own advantages and disadvantages, some of them are:

Advantages of using Command-line Arguments

  • Because we are using the terminal to pass arguments, it is the fastest method.
  • It provides absolute precision and accuracy.

Disadvantages of using Command-line Arguments

  • Not suitable for long arguments because copy/paste is annoying and time-consuming.
  • It is hard for a beginner to learn and remember commands but if you are comfortable using shell then it will be easy to work with these.

Let us write a code that will sum all the arguments that we will give using the Command line.

Using sys.argv to use command-line arguments in Python

Firstly, we need to import the sys module using the following command.

import sys

As we have discussed earlier, sys.argv[0] will be the script name, therefore, we will start taking arguments from sys.argv[1].

def summation(): 
    sum = 0
    for i in sys.argv[1:]: 
        sum = sum + float(i)
    print("No. of arguements are: ",len(sys.argv[1:])) 
    return sum
  • We have defined a function named summation and initializes a variable named sum equal to 0.
  • Next, we loop through sys.argv[1:] which will give all the arguments incrementally and do note that we have used slicing here as [1:] that means from index 1 to end.
  • Because the sys.argv is a list and all elements are in the string format, to implement arithmetic operations, we have typecasted it into the float datatype and are just adding up all the arguments.
  • Next, we are printing all the arguments and return the sum we get after all additions.
  • Its time to call the function, give some command-line arguments and see the results.
ans = summation()
print("Sum of given command line inputs are: ",ans)

The output of the first run

PS D:\VS_code_workspace> python cl_arguments.py 1 3 2 7 10 15 42
No. of arguements are:  7
Sum of given command line inputs are:  80.0

The output of the second run

PS D:\VS_code_workspace> python cl_arguments.py
No. of arguements are:  0
Sum of given command line inputs are:  0

We hope you like this tutorial and if you have any doubts, feel free to leave a comment below.

You may like to learn

Using Bisect Module in Python

Generating QR-Codes in Python using QRCode Library

How to Calculate the Execution Time of a Small Python Program

Leave a Reply

Your email address will not be published.