How to use sys.argv in Python
Last Updated :
02 Aug, 2024
Command line arguments are those values that are passed during calling of program along with the calling statement. Thus, the first element of the array
sys.argv()
is the name of the program itself.
sys.argv()
is an array for
command line arguments in Python. To employ this module named “
sys
” is used.
sys.argv
is similar to an array and the values are also retrieved like Python array.
The sys module
The sys module provides functions and variables used to manipulate different parts of the Python runtime environment. This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter.
Examples:
Python
# Python program to demonstrate
# sys.argv
import sys
print("This is the name of the program:", sys.argv[0])
print("Argument List:", str(sys.argv))
Output:

The above program has been saved by the name “com.py” and thus has to be called in the following in command prompt
Functions that can be used with sys.argv
- len()- function is used to count the number of arguments passed to the command line. Since the iteration starts with 0, it also counts the name of the program as one argument. If one just wants to deal with other inputs they can use (len(sys.argv)-1).
- str()- this function is used to present the array as a string array. Makes displaying the command line array easier and better.
Example:
Python
# Python program to demonstrate
# sys.argv
import sys
print("This is the name of the program:",
sys.argv[0])
print("Number of elements including the name of the program:",
len(sys.argv))
print("Number of elements excluding the name of the program:",
(len(sys.argv)-1))
print("Argument List:",
str(sys.argv))
Output:

The following program performs addition using inputs given during runtime:
Python
# Python program to demonstrate
# sys.argv
import sys
add = 0.0
# Getting the length of command
# line arguments
n = len(sys.argv)
for i in range(1, n):
add += float(sys.argv[i])
print ("the sum is :", add)
Output:

How to use sys.argv in Python – FAQs
What is the use of sys.argv in Python?
sys.argv is a list in Python, which contains the command-line arguments passed to the script. The first element, sys.argv[0], is the name of the script itself. Subsequent elements are the arguments passed to the script.
import sys
# Print the command-line arguments
print(sys.argv)
If you run the script with python script.py arg1 arg2, the output will be:
['script.py', 'arg1', 'arg2']
Can I set sys.argv in Python?
Yes, you can manually set sys.argv in your code for testing purposes. This is useful for simulating command-line arguments during development.
import sys
# Set sys.argv for testing
sys.argv = ['script.py', 'test_arg1', 'test_arg2']
# Print the command-line arguments
print(sys.argv)
How do I run SYS in Python?
To use the sys module, you need to import it at the beginning of your script. You can then use sys.argv to access command-line arguments or other functions from the sys module.
import sys
# Access command-line arguments
print(sys.argv)
Is sys.argv 0 or 1 in Python?
The sys.argv list starts at index 0. sys.argv[0] is the name of the script being executed. Command-line arguments start from sys.argv[1].
import sys
# Print the script name and arguments
print("Script name:", sys.argv[0])
if len(sys.argv) > 1:
print("First argument:", sys.argv[1])
How to test sys.argv in Python?
To test sys.argv, you can set it manually within your script or use a testing framework to simulate command-line arguments.
import sys
# Manually set sys.argv for testing
sys.argv = ['script.py', 'test_arg1', 'test_arg2']
# Function to print command-line arguments
def print_args():
for i, arg in enumerate(sys.argv):
print(f"Argument {i}: {arg}")
# Test the function
print_args()
These examples show how to use, set, run, and test sys.argv in Python, as well as clarify the indexing of command-line arguments.
Please Login to comment...