Categories
COMPUTER SCIENCE DATA SCIENCE PROGRAMMING PYTHON TUTORIALS

How To Read Command Line Arguments In Python

In this article, we will see how to read the command line arguments passed to our Python program. So by the end of this article, you will know how to read the options sent to your program.

Sounds good? Great! Then let us start right away!

So in order for us to be able to read the parameters passed, we will make use of sys module. But before we look into this module, let us first see why want to pass arguments in the first place. Because knowing why we need it will help us understand how we can solve it later. I hope that makes sense!

What Are Command Line Arguments?

So let us start with what command line arguments are and then see how we can read them.

Say you want to write a Python program to add two numbers. Can you tell me what is the simplest code we can write for that? It looks something like this right?

a = 10
b = 20
sum = a + b
print(sum)

And when you run this piece of code, we get an output like this:

30

It is quite straight forward right? We just add the two numbers 10 & 20 and store it in the variable called sum. We then go ahead and print the value of the sum which is 30. So it is all good here. Right?

But there is one problem here. This program is quite worthless! Why? Because every time you run it, it always adds the numbers 10 & 20 to give us the result 30. Not so useful isn’t it?

Read Command Line Arguments
Read Command Line Arguments

So then what do we want? We want to write a Python program that can add two numbers. But we do not want these numbers to be fixed. Instead, we want it to take two input numbers from us and add them.

So what do we have to do for that? We will have to write a program that can take two numbers from us before it runs. So in other words, we will pass the two input values to the program as we call it to run. Now these values that we pass along with the program are called command line arguments!

Alright then! Now that we know what command line arguments are, let us learn how to read these command line arguments into our Python code.

How To Read Command Line Arguments In Python

So as we said in the earlier, reading command line arguments in Python is easy. Because we have a module called sys to do just that!

So using sys module, we can read the two input numbers from the command line. Here is the code for doing just that!

import sys
a = int(sys.argv[1])
b = int(sys.argv[2])
sum = a + b
print(sum)

Okay. Before I explain what is happening here, let us see how to run this code. To run it, first we save this code in a file called sum.py. So if we want to add two numbers 1 & 2, we will run this program by passing these values as shown:

python sum.py 1 2

And when we run it, we get the following output:

3

So as you can see, the code has taken our input values 1 & 2, added them and then finally printed the result. It is doing exactly what we wanted. Right?

Understanding sys.argv

So now let me explain what is going on in our code:

import sys
a = int(sys.argv[1])
b = int(sys.argv[2])
sum = a + b
print(sum)

In the first line, we are just importing the sys module. So nothing big going on there. Right? But what is happening in line number 2 & 3?

Let us break it down a little bit. First let us see what is present inside the parenthesis. So we have the code that looks like this:

sys.argv[1]

Now what is going on here? Well, we are making use of the sys module’s argv list. This is the list which will hold our input parameter values 1 & 2. But then why are we indexing it at 1? Well because index 0 will be pointing to the program name sum.py!

So we take the input values 1 & 2 from argv list and type cast it to integers. But why? Because every element of argv list will be a string. So we need to type cast it as required ourselves. Got it?

So from there on, the rest of the code is pretty straight forward. We will just add the two numbers and print the result!

So there you go! That is how we can read command line arguments in our Python code. Now if you have any questions about it, do let me know in the comments below. I will be more than happy to help!

So until next time, take care! 🙂

Want to learn how to check the file size in Python? Check this out!