Rlz
Rlz

Reputation: 1689

How do I overwrite a line in Python

I have created a simple python program that will count up to 10

What I am trying to achieve is for the program to delete the previous number and print the new number

This is the code that I have created:

import sys
import time
for i in range(10):
    text = "\r" + str(i)
    sys.stdout.write(text)
    sys.stdout.flush()
    time.sleep(1)

Which outputs:

0123456789

Changing the code to have the "\r" after the str(i) didn't work either

import sys
import time
for i in range(10):
    text = str(i) + "\r"
    sys.stdout.write(text)
    sys.stdout.flush()
    time.sleep(1)

Which also resulted in:

0123456789

I was looking for it to count up, and display at the end only the 9 however it doesn't overwrite the previous numbers

Edit:

I am using Windows, Python3

Edit 2:

How to overwrite the previous print to stdout in python? Does not give me a working answer, it still doesn't do what I want

Therefore due to my excellent reasoning it is not a duplicate :P

Upvotes: 0

Views: 1801

Answers (6)

paullb
paullb

Reputation: 4335

Use the end parameter to specify a "\r" as the line ending.

import time
for i in range(10):
    print(i, end="\r")
    time.sleep(1)

Upvotes: 0

Dalen
Dalen

Reputation: 4236

There are some control symbols accepted by virtual terminals. One of them is '\b' that moves a carret for one place back. This one is accepted on Windows too and I will use it in my example below. Unix terminals accept a lot more of controls including color changes and more.



from time import sleep
import sys, os

def clear ():
    os.system("cls" if sys.platform.startswith("win") else "clear")

clear()

s = "1"
sys.stdout.write(s)
for x in range(2, 21):
    sleep(1)
    # Return carret to beginning of line:
    l = len(s)
    s = l*"\b"
    sys.stdout.write(s)
    # Clear line (just in case):
    s = l*" "
    sys.stdout.write(s)
    # Return to the beginning again:
    s = l*"\b"
    sys.stdout.write(s)
    # Write over new text:
    s = str(x)
    sys.stdout.write(s)

Upvotes: 1

davy gillebert
davy gillebert

Reputation: 307

Why not use the standard print of python? Althougth given in many debugger screens this tends to not work. In terminal (ubuntu) it does

import time

for i in range (10):
    print(i, end='\r')
    time.sleep(1)

Probably for stdout something exists too but in this case I usually use the print of python3

Upvotes: -1

Adithya Moorthy
Adithya Moorthy

Reputation: 41

I believe your code should work perfectly fine. The problem should be that you are using a Windows terminal. Try using Linux. Most code does not work in Windows. You can refer this link to know why you have to stop using python on Windows.

This is also an alternate code you can try.

    import time
    for i in range(10):
        text = str(i)
        print(text,end = "\r")
        time.sleep(1)

Upvotes: -1

Balázs Kovacsics
Balázs Kovacsics

Reputation: 701

As an alternative, you can clear the whole window, by using os.system("cls")

Upvotes: 3

Eduardo
Eduardo

Reputation: 641

I would do it like this:

for x in range(10):
    print("{}".format(x), end="\r")

Upvotes: -1

Related Questions