Automaticaly modifying parameters

Viewed 31

I am running a script by typing xxx.py param.par xxx.py is the python script and param.par is the text file where I am defining parameters for the script.

In param.par I have:

x=1
y=0.1
z=10

So, after modifying those numbers I am saving param.par and running the script.

I need to change x from 1-10 (step 1) and y from 0.1-1 (step 0.1). Instead of doing all this manually and saving every time param.par file and then running script is there any option for how this can be automatized in python.

Any suggestions or examples are welcome.

Thanks.

2 Answers

Define whole xxx.py as a function with arguments and run it in 2 for loops.


def x(x, y, z):
    # here throw all your xxx.py code

# use your default z param:
z = 10

for x in range(1, 11, 1):
    for w in range(1, 11, 1):
        y = w/10
        x(x, y, z) # you might use print(x(x,y,z)) if you're returning output in the function 

So afterall you will have every possible combination of x and y used as argument in your function.

If you want to modify a file, you can use

with open("param.par", "w") as f:
    f.write(<yourstring>)

This in combination with a for loop will hopefully solve the issue.

Related