When writing CLI based applications in Python there often comes a time where you are running a loop and it may take a minute or 2 to complete your loop and break out, if the user is sitting and waiting for the task to complete they may become impatient and try to restart the program because it appears to them as if it is hung. This is a perfect case where you would want to give periodic updates to the user to let them know that your program is in-fact running and they need not worry. But how can we do this in a way that is both informative but does not fill up the console with a bunch of junk text.
How Not To:
Multiple Print Statements Generating Multiple Lines
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.
Which gives the output.
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.
Which gives the output.
So as we can see we cut down the chatter significantly but there is still a much better way to do this.
Doing it Properly:
Overwrite the Console Text
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.
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.
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.
Caveats:
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.
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.
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!
2 Comments
Scott
2019-05-31 02:27:40Hey, nice article. I'm just learning python and I'm getting tripped up on the thing where the last output is shorter than the previous so the on-screen result is a mish-mash of the last and second-last output. You acknowledge this is a potential issue but offer no solution? Assuming you know when you get to the last line, what do you have to do to clear the output before writing the last text? Thanks!!!
George Tham
2019-01-25 15:54:13Tried using the "\r" to write over the console text but didn't work. I am using version 3.7.2 (64 bits) in Win10. I have also tried with 3.7.2 (32 bits) and 3.6.5 (64 bits) in Win10 . None seems to work. I am out of ideas. Your help would be much appreciated. Tahnk you.
Leave a Reply