6

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 to run this script say 1000 times and each run write my two text files with new names i.e x1.txt + y1.txt then second run x2.txt and y2.txt.

Thinking about this it seems it might be better to start the whole script with something like

runs=xrange(:999)
for i in runs:

##run the script

and then finish with something that does

    for i in runs:
        filnameA=prefix += "a"+i


open("filnamea.txt", "w").write('\n'.join('\t'.join(x for x in g if x) for g in grouper(7, values)))


    for i in runs:
        filnameB=prefix += "a"+i


open("filnameB.txt", "w").write('\n'.join('\t'.join(x for x in g if x) for g in grouper(7, values)))

Is this really the best way to do it? I bet its not..better ideas?

I know you can import time and write a filename that mathes time but this would be annoying for processing later.

2
  • You run the script 1000 times, THEN you write the results out 1000 times to each of 2 files? Should EACH run write to its own output files before you start the next? Commented Apr 12, 2012 at 23:37
  • each run writes two files. i see your point. Commented Apr 12, 2012 at 23:37

3 Answers 3

4

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))
Sign up to request clarification or add additional context in comments.

Comments

2
import subprocess
import sys

script_name = 'dummy_file.py'
output_prefix = 'out_'
n_iter = 5

for i in range(n_iter):
    output_file = output_prefix + str(i) + '.txt'
    sys.stdout = open(output_file, 'w')
    subprocess.call(['python', script_name], stdout=sys.stdout, stderr=subprocess.STDOUT)

On running this, you'll get 5 output text files (out_0.txt, ..., out_4.txt)

Comments

-1

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

Comments

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.