How can you execute functions with multiple arguments at a terminal?

PythonServer Side ProgrammingProgramming

We first import the sys module. We have to use the argv function of the sys module to fetch the arguments of the function entered at the terminal and execute the function.

Example

#fubar.py
import sys
def print_funcargs(arg1, arg2, arg3):
      print arg1 + ' '+ arg2 + ' ' + arg3
if __name__ == "__main__":
      a = sys.argv[1]
      b = sys.argv[2]
      c = sys.argv[3]
print_funcargs(a,b,c)
print sys.argv

At the terminal if we write

$ python fubar.py  I adore books

Output

I adore books
['fubar.py', 'I', 'adore', 'books']
raja
Published on 07-Dec-2017 13:11:52
Advertisements