Python Sys Argv in Simple Words with Examples

Are you looking for solutions to help you pass the Python arguments in the Command Line script, you may use the sys.argv method. This tutorial is made to give you a simplified discussion of the Python sys.argv topic with examples.

What is Python sys.argv?

The sys.argv() contains the essential list of arguments in the Command Line (CMD). This helps Python programmers who use CMD in passing the arguments to the script.

Let us clarify the explanation through the example below.

Example: main.py file

import sys
print ("Number of arguments:", len(sys.argv), "arguments")
print ("Argument List:", str(sys.argv))

This example code could be in a notepad, sublime, or any text editor file. Then to run the program, we will use the command prompt (CMD).

For the result, the program displayed the output which says:

Number of arguments: 3 arguments
Argument List: ['main.py', 'arg1', 'arg2']

This example just shows the implementation of the sys.argv method, but…

How to use sys.argv in Python?

Python sys.argv calls the arguments that are passed or applied in a program through Command Line.

Therefore, the first entry of the array sys.argv() is the program’s name. Python’s sys.argv() returns an array of command line arguments and it employs the sys module.

In addition, sys.argv is comparable to an array, and its values are available in a similar manner.

What is sys module in Python?

The sys module offers a variety of functions and variables in various parts of the Python runtime environment. It gives you access to the elements and methods that work closely with the interpreter and make it possible to do things with the interpreter.

Example: Using the same procedure from the preceding example, let’s have a good look at the illustration below.

The program saved in main.py contains the following:

import sys
  
print("Program Name:", sys.argv[0])  
print("Argument List:", str(sys.argv))

Then we will run this file to the command prompt.

You may learn the Python bisect module or solve the Python No Module Named Error.

Understanding sys.argv with examples

The language describes sys.argv as a list that contains all CMD arguments. This runs the program and sends these arguments to the script through the command line.

In the preceding examples, sys.argv displays the filename and the len() method displays the number of arguments passed to the script.

Also read: How To Make Pong Game In Python Using PyGame

Example

In this example, we will merge the two ideas. The main.py file must have the following lines of code.

import sys
print ("Program Name: ", sys.argv[0])
print ("Number of Arguments: ", len(sys.argv))
print ("Arguments: " , str(sys.argv))

sys.argv Functions that can be used

The sys.argv can also work with len() and str() functions.

  • len() – is a function used to return the number of arguments that are passed to the command line. This function includes the main file as 1 argument and its counting starts at 0.
  • str() – this function understands an array as a string array. It makes the command line array display simpler and better. 

Now let us have the following examples implementing the len() and the str() functions.

Example 1: The main.py file should have the following lines of code.

import sys
print("Program Name:", sys.argv[0])
print("Number of Arguments with the main file:", len(sys.argv))
print("Number of Arguments without the main file:", (len(sys.argv)-1))
print("Arguments:", str(sys.argv))

Example 2: The main.py file should have the following lines of code.

import sys

add = 0.0
a = len(sys.argv)
for i in range(1, a):
	add += float(sys.argv[i])
print ("Sum:", add)

Python sys.argv(1)

The sys.argv(1) is the first argument you passed to the command line. This argument is next to the main file containing the program.

Let us use the example program to distinguish the number of arguments passed to the CMD.

Example Program

import sys
for x in sys.argv:
     print("Argument: ", x)

Output

Argument:  main.py
Argument:  Python
Argument:  for
Argument:  Free

Python sys.argv list

Python sys.argv list contains all the arguments passed by the program into the command line script. The actions towards these arguments depend on the functions and procedures written on the program. The len(sys.argv) function helps us count the number of arguments passed into the script.

Summary

In summary, this tutorial clarifies that Python sys.argv is useful in passing the list of arguments from the main file to the command line script.

The examples above justify the definition and methods of sys.argv along with the demonstrations of len() and str() functions. You can apply these examples to your computer as long as you have the Python language installed.

Leave a Comment