Sahil Manchanda
Sahil Manchanda

Reputation: 1

Run Command Line Arguments in a loop in Python

I have a program that calls a function from a module and uses command line arguments (sys.argv). I want to call this function multiple times with difference set of values for these arguments. I'm basically trying to use a loop to run this but it is not working:

test_args = [[1,2,3],[4,5,6],[7,8,9]]
i = 0
while i < len(test)args)+1:
    sys.argv = test_args[i]
    module.function()
    i=i+1

I tried for loop and even while loop, but the iteration stops after the first iteration. The first iteration though runs successfully but doesn't proceed to the next iteration i.e i = i+1

Is there a way to run this in loop?

Upvotes: 0

Views: 1905

Answers (1)

Siva Shanmugam
Siva Shanmugam

Reputation: 658

I hope this is what you are trying to achieve.

As you didn't mention the function present in the module. I have created sum function as an example.

Here is the code

import sys #if you directly take input from system arguments
def addall(a):
    return sum(a)
test_args = [[1,2,3],[4,5,6],[7,8,9]]
for i in test_args:
    print (i, addall(i))

Output:

[1, 2, 3] 6
[4, 5, 6] 15
[7, 8, 9] 24

Upvotes: 1

Related Questions