Im printing 2 lines that both use \r and % to replace a either %d or %s in their own print lines. Problem is they seam to get crossed over with one another.
def pinging():
average_list = []
max_list = []
host_list = host_string(ptype)
numberitems = len(host_list)
counter = 0
for i in range(0,len(host_list)):
counter = 1
print('Pinged:%d' % i,"of",numberitems, end='\r')
try:
ping = subprocess.check_output(['ping','-c',num_pings,host_list[i]],timeout=sec_timeout)
except subprocess.TimeoutExpired:
print('Timeout:%s' % host_list[i], end="\r")
continue
ping = str(ping)
ravg,rmax = string_configure(ping,host_list[i],num_pings)
average_list.append(ravg)
max_list.append(rmax)
return(average_list,max_list)
I was expecting 2 separate updating print lines:
1 of 25
#and so one
Timeout: 12.34.56.78
#and so on
Instead i get:
0 of 25
1 of 25.25.109
# it continue like this replacing the "numberitems" with an altered ip address from "host_list"
So how do i stop these 2 print lines from interacting?
CodePudding user response:
Your problem is that \r
only moves the cursor back to the beginning of the line but does not erase it so the next print will just write above what was already written but won't erase the next characters so you will get a mix of the two lines.
"\033[F"
is a special sequence to move your cursor to the line above in the console.
So after the two print
you can add
print("\033[F\033[F", end="")
That will move the cursor two lines up which means that the two lines will be displayed together without one crossing the other.
You should also remove the end="\r"
in your print
if you want this to work.