execute python script multiple times

前端 未结 2 506
终归单人心
终归单人心 2021-01-15 05:19

Im not sure about the best way to do this but I have a python script saved as a .py. The final output of this script is two files x1.txt and y1.txt.

Basically I want

相关标签:
2条回答
  • 2021-01-15 05:34

    If your computer has the resources to run these in parallel, you can use multiprocessing to do it. Otherwise use a loop to execute them sequentially.

    Your question isn't quite explicit about which part you're stuck with. Do you just need advice about whether you should use a loop? If yes, my answer is above. Or do you also need help with forming the filenames? You can do that part like this:

    import sys
    
    def myscript(iteration_number):
        xfile_name = "x%d.txt" % iteration_number
        yfile_name = "y%d.txt" % iteration_number
        with open(xfile_name, "w") as xf:
            with open(yfile_name, "w") as yf:
                ... whatever your script does goes here
    
    def main(unused_command_line_args):
        for i in xrange(1000):
            myscript(i)
        return 0
    
    if __name__ == '__main__':
        sys.exit(main(sys.argv))
    
    0 讨论(0)
  • 2021-01-15 05:46

    I'm not sure, but maybe, it can help: Suppose, I want to print 'hello' 10 times, without manually writing it 10 times. For doing this, I can define a function :

        #Function for printing hello 10 times:
        def func(x):
           x="hello"
           i=1
           while i<10 :
               print(x)
               i += 1
           else :
               print(x)
    
        
        print(func(1))
    
    0 讨论(0)
提交回复
热议问题