Given an instance here.
For Linux, a down-counter can be implemented as:
# work for Linux
import time
for a in [999,55,1]:
print(f'\033[K{a}',end='\r')
time.sleep(1)
But it doesn't work for Windows. The key is that the current line cannot be cleared by print('\033[K'+'Anything', end='\r')
, although it works for Linux.
I know another way using many space symbols:
for a in [999,55,1]:
print(f'{a} ',end='\r') # two space symbols
time.sleep(1)
However it is not always perfect, if the list is changed:
for a in [8888888,77,3]:
print(f'{a} ',end='\r') # space symbols are not enough
time.sleep(1)
And I don't like the followed space symbols.
How to clear the current line in the Windows console with Python? Avoid using packages for simplicity.