Prashant sharma
Prashant sharma

Reputation: 445

How to output to the same line overwriting the previous line?

How to print the output on the same line by overwriting the previously printed Timing(countdown) value?

As shown below, after each second, the timing value is printed on the next row.

13:35:01

13:35:00

13:34:59

13:34:58

13:34:57

13:34:56

I want the timing value to be printed on the same row clearing the previous one.

Upvotes: 34

Views: 78063

Answers (5)

Uik
Uik

Reputation: 1

in Python 3.* use:

var = [some data u want to work with]

for i in range(len(var)):
   # some work stuff
   print("\r"+str((i/len(var))*100)+"%", end="\r", flush=True)
print("\n")

without the "\r" at the beginning Python will just write from the start of the line, but doesn't override the previous text. Notice! it will not clear the prev. line only override it!

u can also use print("\r"+str(int((i/len(var))100*)**)+"%", end="\r", flush=True)

the last print statement is for starting a new line after the loop than u have only integer %-values

Upvotes: 0

tobias_k
tobias_k

Reputation: 82899

You can use the "return"-character \r to return to the beginning of the line. In Python 2.x, you'll have to use sys.stdout.write and sys.stdout.flush instead of print.

import time, sys
while True:
    sys.stdout.write("\r" + time.ctime())
    sys.stdout.flush()
    time.sleep(1)

In Python 3.3, you can use the print function, with end and flush parameters:

    print(time.ctime(), end="\r", flush=True)

Note, however, that this way you can only replace the last line on the screen. If you want to have a "live" clock in a more complex console-only UI, you should check out curses.

import time, curses
scr = curses.initscr()
scr.addstr(0, 0, "Current Time:")
scr.addstr(2, 0, "Hello World!")
while True:
    scr.addstr(0, 20, time.ctime())
    scr.refresh()
    time.sleep(1)
curses.endwin()

Upvotes: 67

user12875967
user12875967

Reputation: 13

Works in python3

But only for windows

import os
import time
while True:
    print(time.ctime())
    time.sleep(1)
    os.system('cls')

Upvotes: -2

This will work like a champ:

print("This text is about to vanish - from first line", end='')
print("\rSame line output by Thirumalai")

output:

Same line output by Thirumalai from first line

Upvotes: 11

himnabil
himnabil

Reputation: 378

I am using python 2.7

python --version
Python 2.7.12 :: Anaconda 4.1.1 (64-bit)

and I am using this the following function as a hook to display the download progress, by using urllib.urlretrieve

def hook(bcount , bsize, tsize ):
    str = "[ %3d%% of %10d] \r" % (bcount * bsize * 100/tsize , tsize)    
    print str,

urllib.urlretrieve (url, file_name, hook)

Explanation: the \r put the cursor at start of the line , and the comma avoid to print in a new line , if you have the same number of character for each print , this will do the trick

if you are curious about urllib and the hook I am using you'll find the doc https://docs.python.org/2/library/urllib.html

Upvotes: 2

Related Questions