Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 13 Online
OpenStudy (anonymous):

essentially I need to generate a file with 10 rows. x = 0 fout = open("store.txt", "w") while x <= 10: fout.write(x) x = x + 1 fout.close() This of course doesn't work. I need it to write value of x into file, but whichever way I try doing it ((x),('x'),("x")) the result is not what I need.

OpenStudy (anonymous):

what gets written to your file?

OpenStudy (anonymous):

just want a file with 1000000 rows, with numbers from 1 - 1000000 in each row. like this: 1 2 3 4 ... 1000000

OpenStudy (anonymous):

Do you mean like this mate: http://codepad.org/Tgcju5iA . I will attach my .txt file from running this code.

OpenStudy (anonymous):

how about: fout.writelines([str(x) for x in xrange(1, 1000001)])

OpenStudy (anonymous):

bmp: What does your if loop do there ? not sure about the os.path.isdir part if os.path.isdir("null.txt"): continue fname.write(str(x)+"\n")

OpenStudy (anonymous):

bwCA: cool ! this looks really impressive, just one line with all the stuff. Cheers !

OpenStudy (anonymous):

This was the template for a similar program and that if is a leftover. It was just to check whether or not the file is in the correct path, instead of raising an error.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!