The arguments that are given after the name of the program in the command line shell of the operating system are known as Command Line Arguments. Python provides various ways of dealing with these types of arguments. One of them is sys module.
sys Module
A module is a file containing Python definitions and statements. 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.
sys.argv
sys.argv is used in python to retrieve command line arguments at runtime. For a program to be able to use it, it has to be imported from the “sys” module. The arguments so obtained are in the form of an array named sys.argv.
Note: sys.argv[0] gives the name of the program and the following indices work like members of an array.
Approach:
The name of the program is “cmdlist.py”.
- Working with command line: Consider the below code written in cmdlis.py
importsysprint("the name of the program is ", sys.argv[0])print("argument list :", sys.argv)chevron_rightfilter_noneOutput:
- Calling a list using the command line
importsysprint("the name of the program is ", sys.argv[0])a=sys.argv[1]print(a)chevron_rightfilter_noneOutput:
- Working with the list called by command line.
sys.argv is a string array. Thus, any argument passed to it is a string so in order to properly put in use we have to convert it to an appropriate list form.- Program to take list as command line argument and convert it to a proper list form
importsysprint("the name of the program is ", sys.argv[0])n=len(sys.argv[1])a=sys.argv[1][1:n-1]a=a.split(', ')foriina:print(i)chevron_rightfilter_noneOutput:

- Program to find the sum of all the members of the list
Also, keep in find to change the members to the required data type if not string.importsysprint("the name of the program is", sys.argv[0])n=len(sys.argv[1])a=sys.argv[1][1:n-1]a=a.split(',')A=[int(i)foriina]b=0foriinA:b+=iprint("sum of all the list members is ", b)chevron_rightfilter_noneOutput:
- Program to take list as command line argument and convert it to a proper list form
- Working with multiple list retrieved through command line.
importsysprint("the name of the program is ", sys.argv[0])n=len(sys.argv[1])a=sys.argv[1][1:n-1]a=a.split(', ')A=[int(i)foriina]b=0foriinA:b+=iprint("sum of all the list members is ", b)n=len(sys.argv[2])c=sys.argv[2][1:n-1]c=c.split(', ')d=""foriinc:d=d+iprint("sum of all the list members is ", d)chevron_rightfilter_noneOutput:
Recommended Posts:
- Command-Line Option and Argument Parsing using argparse in Python
- How to pass argument to an Exception in Python?
- Python | Split and Pass list as separate parameter
- Passing function as an argument in Python
- Python Lambda with underscore as an argument
- Read a file line by line in Python
- Command Line Interface Programming in Python
- Python | Set 6 (Command Line and Variable Arguments)
- Click Module in Python | Making awesome Command Line Utilities
- Command Line Scripts | Python Packaging
- Python | How to Parse Command-Line Options
- Command Line Arguments in Python
- Argparse VS Docopt VS Click - Comparing Python Command-Line Parsing Libraries
- How to write an empty function in Python - pass statement?
- Loops and Control Statements (continue, break and pass) in Python
- Difference between continue and pass statements in Python
- break, continue and pass in Python
- Pass by reference vs value in Python
- Python pass Statement
- Python - pass multiple arguments to map function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

