[Python] I am trying to write numbers 1 - 4096 in a .TXT file using a For Loop, but when the...

Discussão em 'Python' iniciado por Stack, Julho 22, 2025.

  1. Stack

    Stack Membro Participativo

    I have 2 files:

    text.txt

    And:

    main.py

    In main.py, I have the following 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 I cannot write an int() (i) in a text file.

    Thanks

    Daniel

    Continue reading...

Compartilhe esta Página