
In the command line shell of the operating system, the arguments that are given after the name of the program are known as Command Line Arguments. We know that Python Command line arguments are input parameter passed to the script when executing them in sequence. In this programming world mostly all programming language provide support for command line arguments function.

Python language provides various ways of dealing with these types of arguments.
Table of Contents
command line arguments in python
There are some options to read python command line arguments. In this article we are seeing the three most common ones are:
- Using sys.argv
- Using getopt module
- Using argparse module
Let’s look through simple program to learn how to read and use python command line arguments.
# program for command line arguments in python
import sys
print(type(sys.argv))
print('Example of command line arguments are:')
for i in sys.argv:
print(i)
import sys
# Passed total arguments
n = len(sys.argv)
print("Passed total arguments:", n)
# Arguments passed
print("\nname of script:", sys.argv[0])
print("\narguments passed:", end = " ")
for i in range(1, n):
print(sys.argv[i], end = " ")
# Add. of numbers
Sum = 0
Using argparse module
for i in range(1, n):
Sum += int(sys.argv[i])
print("\nresult:", Sum)
OUTPUT:
name of script: pythonslearning.py
arguments passed: 4 5 3 2
result: 14
Python getopt module
Python getopt module is like as the C getopt() function.This module for parsing command-line parameter where we want user to enter some options too it.
Also see security update about python
Let’s see at a simple example to understand this.
import getopt
import sys
argv = sys.argv[1:]
try:
opts, args = getopt.getopt(argv, 'hm:d', ['help', 'my_file='])
print(opts)
print(args)
except getopt.GetoptError:
# Print a message or do something useful
print('Something went wrong happen!')
sys.exit(2)
In this above example getopt module will automatically parse the option value and map them.
Python argparse module :
This module is the one way to parse command line arguments. It provides a lot of more option such as default value for arguments, help message, positional arguments, specifying data type of argument etc. we can use python argparse module like below.
import argparse
parser = argparse.ArgumentParser()
parser.parse_args()
Python argparse module provide a lot many features like that.
So finally we are take one more example : Python script for adding two numbers and the numbers are passed as command-line arguments.
Some FAQ about command line arguments in python :
In the command line shell of the operating system, the arguments that are given after the name of the program are known as Command Line Arguments. Most programming languages provides support for command line arguments.
We know that Python Command line arguments are input parameters passed to the script when executing them in sequence.
Use the following command line:
sys. argv and exec().This command line used to execute a file with arguments.
we can access it using sys.argv. sys. argv method is a very useful and simple way to read command line arguments as String.
The argument is a value passed to a method or a function when calling the function. Basically, arguments are classified into five types :
1. keyword arguments.
2. positional arguments.
3. arbitrary positional arguments.
4. arbitrary keyword arguments.
5. default arguments.
conclusion :
In this article we saw the different types of argument in Python like 1. keyword arguments.
2. positional arguments.
3. arbitrary positional arguments.
4. arbitrary keyword arguments.
5. default arguments.
so about this section you have any query then please comment below.
If you want to learn event handling in python tutorials point then click on it.