Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing arguments to functions in python using argv

Please find the below script

from sys import argv
script, contact_name, contact_no = argv

def operation()
    some code goes here

Since i run this script from command line, How do i pass contact_name and contact_no to operation function? I am using python 2.7

like image 995
louis Avatar asked Jan 28 '16 06:01

louis


1 Answers

Command line arguments are passed as an array to the program, with the first being usually the program's location. So we skip argv[0] and move on to the other arguments.

This example doesn't include error checking.

from sys import argv

def operation(name, number):
    ...

contact_name = argv[1]
contact_no = argv[2]

operation(contact_name, contact_no)

Calling from command line:

python myscript.py John 5
like image 61
jacob Avatar answered Sep 27 '22 21:09

jacob