top of page

Python Tutorial

Public·1 member

Command line arguments in Python


sys.argv is used to access command line argument in python


sys.argv is the list of commandline arguments passed to the Python program.
argv represents all the items that come along via the command line input.

import sys
program_name = sys.argv[0]
arguments = sys.argv[1:]
no_of_args = len(arguments)
print("Program name: ",program_name)
print("Number of argument: ",no_of_args)

for i in range(no_of_args):
     print(i, sys.argv[i])

Run:

$ python test.py arg1 arg2 arg3

output:

Program name:  test.py
Number of argument: 4
test.py
arg1
arg2
arg3



17 Views
bottom of page