0

I have this code:

def ranges(ip):
    try:
        part = ip.split('.')
        a = part[0]
        b = part[1]
        c = part[2]
        d = part[3]
        for c in range(1, 256):
                 for d in range(1, 256):
                     ip_result = (str(a) + '.' + str(b) + '.' + str(c) + '.' + str(d))
                     print(ip_result, end='\r')
                     open('IpRanged.txt','a').write(ip_result+"\n")
    except:
        ... 

It is supposed to show each output on the same line of the terminal, but it does not properly show every output. What is wrong, and how do I fix it?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
New Id
  • 1
  • You want to print the generated IP address in a single line which is updated every iteration? Than it works like you have done. You see not every print, because it is so fast, that you can not see all prints... Notes to your code: The `try` is missing an `except`, `part[2]` and `part[3]` are never used. – phibel Jan 14 '23 at 12:29
  • Also, your code produces an error as you didn't include an except statement. – Nonlinear Jan 14 '23 at 12:30
  • Yes II just did not upload the full code which has except statement and that part[0] and part[1] have some work but not here used – New Id Jan 14 '23 at 12:33
  • So is there any way that it can print in one line without buffering – New Id Jan 14 '23 at 12:34
  • 1
    `print(ip_result, end='\r', flush=True)` https://docs.python.org/3/library/functions.html?highlight=print#print – phibel Jan 14 '23 at 12:41
  • You want a fast counting smooth print of your generated IP addresses? Maybe some of these example codes get you the desired result: https://stackoverflow.com/questions/4897359/output-to-the-same-line-overwriting-previous-output – phibel Jan 14 '23 at 12:53
  • Welcome to Stack Overflow. Please see the linked duplicate. If the output ends with a carriage return (`end='\r'`), then it doesn't end with a newline, causing the problem described there. For future questions, though, please read [ask] and try to **ask a question** - "Please help me" [does not qualify](https://meta.stackoverflow.com/questions/284236), and we [don't like](https://meta.stackoverflow.com/questions/343721) smilies here as this is **not a discussion forum**. We **do** want complete English sentences with proper punctuation and grammar. I edited the question to lead by example. – Karl Knechtel Jan 14 '23 at 12:55

0 Answers0