Search code examples
pythonloopsexecute

python: execute a .py file multiple times?


I have about 50 lines of code, which isn't based on any looping variable (which is why I cannot do for loop) but I want to run this code several times so that my output array keeps building up in length. Is there any way to do that? I would like to execute this .py file 10,000 times. Let me know how I can do that. I know normally I would execute it on my terminal by typing

execfile('filename.py')

but how to do this several times? Thank you!

example of what's in the .py file:say I have a bunch of distributions I want to calculate chi sq value for, and save all the values in an array chi = [] after the 10,000 iterations, how would I do that for this case?

chi = []
def chisqg(ydata,ymod,sd=None):  

    if sd==None:  
        chisq=np.sum((ydata-ymod)**2)  
    else:  
        chisq=np.sum( ((ydata-ymod)/sd)**2 )  
    return chisq

chi1 = chisqg(y1,mod1,sd=0.1)
chi.append(chi1)

Solution

  • I'm not sure why you think a loop won't work here; if you want your chi variable to end up with 10k entries in it you can just change the last lines of your file to:

    for i in range(10000):
        chi1 = chisqg(y1,mod1,sd=0.1)
        chi.append(chi1)