How do I write output in same place on the console?
Python 2
I like the following:
print 'Downloading File FooFile.txt [%d%%]\r'%i,
Demo:
import time
for i in range(100):
time.sleep(0.1)
print 'Downloading File FooFile.txt [%d%%]\r'%i,
Python 3
print('Downloading File FooFile.txt [%d%%]\r'%i, end="")
Demo:
import time
for i in range(100):
time.sleep(0.1)
print('Downloading File FooFile.txt [%d%%]\r'%i, end="")
PyCharm Debugger Console with Python 3
# On PyCharm Debugger console, \r needs to come before the text.
# Otherwise, the text may not appear at all, or appear inconsistently.
# tested on PyCharm 2019.3, Python 3.6
import time
print('Start.')
for i in range(100):
time.sleep(0.02)
print('\rDownloading File FooFile.txt [%d%%]'%i, end="")
print('\nDone.')
You can also use the carriage return:
sys.stdout.write("Download progress: %d%% \r" % (progress) )
sys.stdout.flush()