0

i'm making a number list generator and i want to print the output as a list in a .txt file but it always just prints the final value for the variable. here's my code :

zero = ["000000","00000","0000","000","00","0",""] for x in xrange(0,999999): with open("Output.txt", "w") as text_file: text_file.write("011"+zero[len(str(x))] + str(x)+"70") 
2

2 Answers 2

4

You're reopening the file for each number. Every time you do that, it empties the file unless you use a mode.

You should open the file once, then do the loop inside that.

with open("Output.txt", "w") as text_file: for x in xrange(0,999999): text_file.write("011"+zero[len(str(x))] + str(x)+"70") 
Sign up to request clarification or add additional context in comments.

Comments

0

Opening a file for writing erases the file first. Open the file then loop.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.