Creating 50K text files in python

John Machin sjmachin at lexicon.net
Wed Mar 18 10:21:23 EDT 2009


On Mar 19, 12:50 am, "venutaurus... at gmail.com"
>         FName = "TextFile"+c+"_"+d+"_"+p+".txt"
>         l =1
>         for l in range(1 , 11):
>             os.system ("\"echo "+FName+" >> "+FName+"\"")
>             l = l +1

That is not the most clear code that I've ever seen. Makeover time!
os.system ("\"echo "+FName+" >> "+FName+"\"")
os.system('"echo ' + FName + ' >> ' + FName + '"')
os.system('"echo %s >> %s"') % (FName, FName)

Some questions:
(1) Isn't that one level of quoting too many?
(2) Does os.system create a whole new shell process to execute that
command?
(3) Can you think of a better/faster way to write 10 lines to a file
than doing os.system in a loop?
(4) Why are you incrementing the loop variable yourself?
(5) Why are you setting the loop variable to 1 before the loop?
(6, 7) as for (4, 5) but this time in relation to the enclosing "for
k..." loop.
(8) Did you actually test that this script was working on some *small*
number of files before you fired it off?



More information about the Python-list mailing list