Overwriting Line of Console Output – Python

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!

9 Comments

  1. Your Python/Tkinter tutorials are the best I’ve come across. New to Python and Tkinter finding clear explanations that apply to Python 3.6 and Tkinter 8 is confusing with so many sites using the older versions of both. I was stuck on grid whitespace in particular. None of the test code worked for me, until I read your explanation.

    Keep them comming!

  2. I tried this, and it’s what many other websites say work perfectly, I just can’t get it to work myself. I put together some code like this:
    import time
    print(“Hello”, end=’\r’, flush=True)
    time.sleep(1)
    print(” im back”)

    The output always comes out as:
    hello im back

    Am I doing something wrong?
    P.S. I’m using IDLE 3.7 on Windows

    • Are you running the code directly in idle or putting the code in a file and running that file from the command line?

          • Nope, gives the same exact output. I think that the IDLE shell doesn’t support ANSI escapes, which kinda sucks.

          • You said that your pasting the code into a .py file and running it so it should not be running it in idle. If you are running the python file from the command line you should have no issues, I have tested it. Try running the file from cmd.exe

          • Welp, that was the problem. I was just editing the file with IDLE and pressing F5 to run it. Just double-clicking the .py file works perfectly! I’ll keep that in mind in the future. Also, I think it’s great what you’re doing, and thanks for responding so quickly. Keep up the good work!

Leave a Reply

Your email address will not be published.


*