The problem is that when the previous printed string is bigger than the second one, some stuff of old print will reamins in the new print.
Here's the full code:
import time
long_string = "AAAAA"
short_string = "BB"
c = 0
while True:
if c % 2 == 0:
print(long_string, end="\r", flush=True)
else:
print(short_string, end="\r", flush=True)
c += 1
time.sleep(1)
I would expect that in the if it would print "AAAAA" and in the else it would print just "BB", but instead, in the else, it prints "BBAAA" because the "A" remains from previous print. Here's what I mean. I would like it would be just "BB" instead of "BBAAA".
I saw this post answer Python: How to print on same line, clearing previous text? so I tried to use os.sys.stdout.write('\033[K' + long_string + '\r') instead of print, but I still have the same problem and it also print a strange character
How can I completely delete previous clean and make a new one without traces of the old one?
Edit:
@Thierry Lathuille
Here's the full code with the second solution:
import time, os
long_string = "AAAAA"
short_string = "BB"
c = 0
while True:
if c % 2 == 0:
os.sys.stdout.write('\033[K' + long_string + '\r')
else:
os.sys.stdout.write('\033[K' + short_string + '\r')
c += 1
time.sleep(1)
It produces this output
I'm testing it in Windows 10 cmd.
Edit 2:
I said that the solution of "\r" didn't work and you closed my question and said that it has been answered in that thread. Are you kidding me right? Please open it again, that solution doesn't work, I already said it, that thread's answeres doesn't work to me and I explained why.
0 comments:
Post a Comment
Thanks