The accepted answer is not perfect. The line that was printed first will remain there, and if your second print does not cover the entire new line, you will get garbage text.
To illustrate the problem, save this code as a script and run it (or just look):
import time n = 100 for i in range(100): for j in range(100): print("Progress {:2.1%}".format(j / 100), end="\r") time.sleep(0.01) print("Progress {:2.1%}".format(i / 100))
The output will look something like this:
Progress 0.0%% Progress 1.0%% Progress 2.0%% Progress 3.0%%
What works for me is to clear the line before leaving a permanent print. Feel free to adapt to your specific problem:
import time ERASE_LINE = '\x1b[2K'
And now it prints as expected:
Progress 0.0% Progress 1.0% Progress 2.0% Progress 3.0%
mimoralea Jul 17 '18 at 20:48 2018-07-17 20:48
source share