The Wayback Machine - https://web.archive.org/web/20240809135130/https://www.geeksforgeeks.org/how-to-use-sys-argv-in-python/
Open In App

How to use sys.argv in Python

Last Updated : 02 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
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:

sys.argv

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:

sys.argv

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:

sys.argv

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.



Similar Reads

Python - sys.settrace()
Python sys module provides some of the powerful functions but they are complex to understand. One of which is the sys.settrace() which is used for implementing debuggers, profilers and coverage tools. This is thread-specific and must register the trace using threading.settrace(). Knowing how the function works internally is really important in case
3 min read
Python | sys.getallocatedblocks() method
This sys module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It provides information about constants, functions and methods of python interpreter. It can be used for manipulating Python runtime environment. sys.getallocatedblocks() is used to get number of memo
1 min read
Python | sys.getrecursionlimit() method
This sys module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It provides information about constants, functions and methods of python interpreter. It can be used for manipulating Python runtime environment. sys.getrecursionlimit() method is used to find the cur
1 min read
Python | sys.getdefaultencoding() method
This sys module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It provides information about constants, functions and methods of python interpreter. It can be used for manipulating Python runtime environment. sys.getdefaultencoding() method is used to get the cur
1 min read
Python | sys.setrecursionlimit() method
This sys module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It provides information about constants, functions and methods of python interpreter. It can be used for manipulating Python runtime environment. sys.setrecursionlimit() method is used to set the maxi
2 min read
Python | sys.setswitchinterval() method
This sys module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It provides information about constants, functions and methods of python interpreter. It can be used for manipulating Python runtime environment. sys.setswitchinterval() method is used to set the inte
2 min read
Python | sys.getswitchinterval() method
This sys module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It provides information about constants, functions and methods of python interpreter. It can be used for manipulating Python runtime environment. sys.getswitchinterval() method is used to get the inte
1 min read
Python - sys.stdout.flush()
A data buffer is a region of physical memory storage used to temporarily store data while it is being moved from one place to another. The data is stored in a buffer as it is retrieved from an input device or just before it is sent to an output device or when moving data between processes within a computer. Python's standard out is buffered. This m
3 min read
sys.maxint in Python
In programming, maxint/INT_MAX denotes the highest value that can be represented by an integer. In some cases, while programming, we may need to assign a value that is larger than any other integer value. Normally one assigns such values manually. For example, consider a list of integers where the minimum value has to be found out using a for loop.
4 min read
sys.stdout.write in Python
This is a built-in Python module that contains parameters specific to the system i.e. it contains variables and methods that interact with the interpreter and are also governed by it. sys.stdout A built-in file object that is analogous to the interpreter's standard output stream in Python. stdout is used to display output directly to the screen con
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg