Posted on 09-20-2018 by superadm1n
Your first thought might be to just insert a print line inside of your loop and let them know how far along they are, a basic example of that is in the code below.
#!/usr/env/bin python3
import time
for n in range(1, 11):
print('I am on number {} of 10'.format(n))
time.sleep(.5)
Which gives the output.
$ python test.py
I am on number 1 of 10
I am on number 2 of 10
I am on number 3 of 10
I am on number 4 of 10
I am on number 5 of 10
I am on number 6 of 10
I am on number 7 of 10
I am on number 8 of 10
I am on number 9 of 10
I am on number 10 of 10
$
As you can see this is generating a bunch of output and looks ugly, A second thought to cut down on the console chatter you could give a periodic update and only print every 5 times the loop runs such as the example below shows.
#!/usr/env/bin python3
import time
for n in range(1, 11):
# if n is a multiple of 5 it will print
if n % 5 == 0:
print('I am on number {} of 10'.format(n))
time.sleep(.5)
Which gives the output.
$ python test.py
I am on number 5 of 10
I am on number 10 of 10
$
So as we can see we cut down the chatter significantly but there is still a much better way to do this.
By default in Python 3 the default end character when using the print() method is a ‘\n’ or ‘newline’ character, if we change this to a ‘\r’ or ‘return’ character when we issue our next print statement it will overwrite the last line, thus not causing a new line of output but rather overwriting the previous output.
In order to change the newline character you just need to define the end character within the print statement, modifying the first example take a look at the line with the print statement in how we defined the end character.
#!/usr/env/bin python3
import time
for n in range(1, 11):
print('I am on number {} of 10'.format(n), end='\r')
time.sleep(.5)
We can see we changed the end character to a ‘return’ character by defining the ‘end’ parameter
There is no good way for me to demonstrate how this code executes on a static web page as it is dynamic so you are best pasting this code into a file and running it yourself. One thing you will notice is that after your last line update prints out “I am on number 10 of 10” when being returned to your prompt your output is overwritten. This is because the last line of your output was a return character so even tho printing your prompt to your console upon exit isn’t a part of your program it will still overwrite your text.
Often times this is no big deal but if you want that last update to stay persistent on the console you will need to find a way to deduce when your loop will be last run and upon exit doing a standard print without modifying the end character, in our example this is simple, we know it will run 10 times so we can run a comparison against n like in the example below.
#!/usr/env/bin python3
import time
for n in range(1, 11):
if n != 10:
print('I am on number {} of 10'.format(n), end='\r')
else:
print('I am on number {} of 10'.format(n))
time.sleep(.5)
Again the best way for you to see the output of this will be to run the code yourself, but you will see that after our last update, when our prompt is returned our last line of output will stay persistent.
The one thing you will possibly run into that can cause confusion is if at some point when overwriting the previous line, your current print out will be shorter than your previous causing the tail end of your previous text to stay on the console causing confusion.
In the example code below I show what would happen in that instance.
#!/usr/env/bin python3
import time
for n in range(1, 3):
if n != 2:
print('This is my longer text printing to the console', end='\r')
else:
print('This is the shorter text')
time.sleep(.5)
After this code completely runs we can see the line we are left with has remnants of the first line of output but the first half has been overwritten by our second, shorter output.
$ python test.py
This is the shorter textrinting to the console
$
With this information on how to overwrite a line of console output you can do fancy things with your CLI scripts incorporating things such as spinning wheels or status bars. If anything in this article was unclear or is not working when your running the code, drop a line in the comments and I will do my best to help out. Lastly if this helped you out please share the post on social media!