[Solved] Python only writing the last digit of a For statement in a txt file

Daniel Asks: Python only writing the last digit of a For statement in a txt file
I have 2 files:

text.txt

And:

main.py

In main.py, I have the following code:

Code:
for i in range(1,4097):
    i = str(i)
    file = open("text.txt","w")
    file.write(i)

As you can see, I want to generate all numbers from 1 – 4096, but when I run the script, it only writes 4096 in text.txt.

How can I write numbers 1 – 4096 inside text.txt?

(By the way, I made i a string (str()) because it have me and Error saying Python can only Write Strings not Intigers:

Code:
Traceback (most recent call last):
  File "d:pathapp.py", line 14, in <module>
    file.write(i)
TypeError: write() argument must be str, not int

)

Thanks

D a n i e l

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.