简体   繁体   中英

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 ie i = i+1

Is there a way to run this in loop?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM