-3
import time

while True:
  for x in range(1, 1000, 1) :
    link = ("example.com/" + str(x))  
    print(link)
    time.sleep(0.1)

Here is the code, I need the link variable to be written to a file like

example.com/1
example.com/2
example.com/3

and so on

The problem i ran into before posting this is that when I write something to a file (example.com/1) in the next cycle the text gets replaced by the new output (example.com/2). Is there a way for me to write to the file in such a way that the script wouldnt replace the already existing lines and start from a new one every cycle

P.S this is my first post and im new to python, pls dont attack me if I did anything wrong!

Dead Dog
  • 15
  • 3
  • 1
    Wow a little harsh for someone's first post. @martineau Try looking up writing to a file in python and implementing that into your code if you still need help after that edit your question with your attempt – digitaluniverse Apr 27 '22 at 18:42
  • What is your question? What part of this do you need help with? Do you know how to open a file and write to it? If not, that's a good place to start with a google search. If it is something else, please give more detail. – Code-Apprentice Apr 27 '22 at 18:44
  • @digitaluniverse: My comment very gentle IMO. – martineau Apr 27 '22 at 18:49
  • @Code-Apprentice I need a file in which there is a list ranging from example.com/1 example.com/2 to example.com/999 – Dead Dog Apr 27 '22 at 18:54
  • 1
    @DeadDog That is a statement. Not a question. What do you actually need help with from what you have so far? What does your current code do? What do you want it to do differently? Have you done any research into the next steps? If so, what did you find and what do you need clarified? – Code-Apprentice Apr 27 '22 at 18:59
  • @Code-Apprentice , the problem i ran into before posting this is that when I write something to a file (example.com/1) in the next cycle the text gets replaced by the new output (example.com/2). Is there a way for me to write to the file in such a way that the script wouldnt replace the already existing lines and start from a new line every cycle? – Dead Dog Apr 27 '22 at 19:19
  • @DeadDog You should show the code that does that and ask a specific question about that particular problem. – Code-Apprentice Apr 28 '22 at 12:05

1 Answers1

1

There are a few ways you could go about this.

Depending on your environment, you can likely redirect your output to a file:

program.py > out.txt

Would output what you print() to out.txt (instead of the screen).

This tutorial will help you with writing (and reading in python).

To write to a file in python, you need to get a handle to the file using open:

writer = open('dog_breeds.txt', 'w')

(the 'w' means open for writing, it will overwrite any existing contents).

Once, you have the handle, you can write() to it, which is very similar to print():

writer.write("example.com/" + str(x))  

Lastly, you need to close the file:

writer.close()

You should be able to modify your existing snippet to make use of the above, and refer to the link to go a bit deeper (including error checking).

John Carter
  • 6,752
  • 2
  • 32
  • 53
  • You're only encouraging more low-quality question like this one by answering it and providing a file-writing tutorial. – martineau Apr 27 '22 at 18:46