-1

So I have a python programme which looks something like this:

import blah blah balh

def main():
    blah blah blah 
    blah blah blah

if __name__ == "__main__":
    main()

The programme takes 2 arguments via eclipse.

But now I would like to run this programme in windows control prompt in a loop and change the 2 arguments dynamically.

I would like to do something like this:

arg1 = [a,b,c,d]
arg2 = [a,b,c,d]

for idx in range(0, len(mtds)):
    #pass in args to programme
    programme(mtds[idx], mdd[idx])

Is this possible?

Apologies in advance, I'm totally ignorant about this.

Boosted_d16
  • 13,340
  • 35
  • 98
  • 158

2 Answers2

3

Here is how you loop in CMD:

for /l %x in (1, 1, 100) do (
   echo %x Prints the current iteration
   python myPythonscript.py input1 input2
)

Starts at 1, steps by 1 and ends at 100.

Stiffo
  • 818
  • 6
  • 19
  • do I not have to use a line like "eclipse.exe" somewhere in that code? o ipy64? sorry Im totally ignorant on this front – Boosted_d16 Jul 29 '15 at 11:14
  • No, not really. It is not eclipse that is executing your script, but rather the python interpreter, same as how eclipse would run the cygwin compiler for you when coding C/C++. "python myPythonScript.py input1 input2" would launch the script "myPythonScript.py" with the given variables input1 and input2. Eg. you could there give arrays or strings as input. – Stiffo Jul 29 '15 at 11:23
  • ahh okay, I got it working! So input1 and input2 takes a single string, how do I use an array to replace whats being passed in these two arguements? – Boosted_d16 Jul 29 '15 at 13:50
  • I believe it will always be treated as a string, so you would need to convert it into an array, eg. by checking for arguments and then converting the specific argument. Here's a post about it: https://stackoverflow.com/questions/7605631/passing-a-list-to-python-from-command-line – Stiffo Jul 29 '15 at 13:59
1

If you want to loop your programm's main() function for a certain amount of times with 2 arguments then you can add some strings:

import sys
first_arg = sys.argv[1]
second_arg = sys.argv[2]
times_to_loop = sys.argv[3]
import blah blah balh

def main():
    blah blah blah 
    blah blah blah

for i in range(int(times_to_loop)):
    main(first_argv, second_argv)

and run your programm from cmd as python programm.py 1 2 10.

This will run your programm 10 times with 1 as first argument and 2 as second

Andersson
  • 51,635
  • 17
  • 77
  • 129