Creating 50K text files in python

S Arrowsmith sion at paintbox.UUCP
Wed Mar 18 15:24:55 CET 2009

venutaurus539 at <venutaurus539 at> wrote:
>        FName = "TextFile"+c+"_"+d+"_"+p+".txt"
>        l = 1
>        for l in range(1 , 11):
>            os.system ("\"echo "+FName+" >> "+FName+"\"")
>            l = l +1

1. os.system spawns a new process, which on Windows (I'm guessing
you're on Windows given the full path names you've given) is a
particularly expensive operation.

2. You really do have a problem with loops. Not how many your
task has landed you, but the way you're writing them. You
don't need to pre-assign the loop variable, you don't need to
increment it, and it's generally clearer if you use a 0-based
range and add 1 when you use the variable if you need it to be

So rewrite the above as:

fname = "TextFile%s_%s_%s.txt" % (c, d, p)
f = open(fname, 'a')
for l in range(10):


3. All the logging with m.write is likely to be slowing things
down. It might be useful to see just how much of an impact that's

4. Lose the globals and pass them as function arguments.

   under construction

More information about the Python-list mailing list