-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.

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

do I not have to use a line like "eclipse.exe" somewhere in that code? o ipy64? sorry Im totally ignorant on this front
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.
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?
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: stackoverflow.com/questions/7605631/…
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

1 Comment

This was the first thing I did however I the programme file to remain totally fixed.no new code or edits.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.