17

I have wanted for a long time to find out how to clear something like print("example") in python, but I cant seem to find anyway or figure anything out.

print("Hey")
>Hey

Now I need to clear it, and write some new text.

print("How is your day?")

It would print.

Hey

>How is your day?

But I want to clear the "Hey" so the user shouldnt look at both at same time, and it looks kinda messy.

braX
  • 11,506
  • 5
  • 20
  • 33
Joel Madsen
  • 179
  • 1
  • 1
  • 9
  • 2
    Once you are wrote to`Console` you cannot erase it, when you are holding it in a buffer you may do that. [NOTE] `clear` in `linux` does not erases your written text, rather it `scrolles` up !! – Siva Cn Oct 25 '13 at 18:15
  • I think this is what you are looking for http://stackoverflow.com/questions/4810537/how-to-clear-the-screen-in-python – Bartosz Dabrowski Oct 25 '13 at 18:15
  • possible duplicate of [clear terminal in python](http://stackoverflow.com/questions/2084508/clear-terminal-in-python) – mdml Oct 25 '13 at 18:18

5 Answers5

19
import os
os.system('cls')

Or os.system('clear') on unix (mac and linux). If you don't want the scroll up either, then you can do this:

os.system("printf '\033c'") should get rid of scroll back too. Something that works on all systems:

import os
os.system('cls' if os.name == 'nt' else "printf '\033c'")
Games Brainiac
  • 80,178
  • 33
  • 141
  • 199
18

I think this is what you want to do:

take the cursor one line up and delete the line

this can be done like using the code below

import sys
import time

def delete_last_line():
    "Use this function to delete the last line in the STDOUT"

    #cursor up one line
    sys.stdout.write('\x1b[1A')

    #delete last line
    sys.stdout.write('\x1b[2K')


########## FOR DEMO ################
if __name__ == "__main__":
    print("hello")
    print("this line will be delete after 2 seconds")
    time.sleep(2)
    delete_last_line()
####################################
Aniket Navlur
  • 962
  • 1
  • 11
  • 22
4

Small addition into @Aniket Navlur 's answer in order to delete multiple lines:

def delete_multiple_lines(n=1):
    """Delete the last line in the STDOUT."""
    for _ in range(n):
        sys.stdout.write("\x1b[1A")  # cursor up one line
        sys.stdout.write("\x1b[2K")  # delete the last line
alper
  • 2,919
  • 9
  • 53
  • 102
2

Well I have a temporary way:

print("Hey", end="")
for i in range(4):
    print('\b', end = '')

print("How is your day?")
alper
  • 2,919
  • 9
  • 53
  • 102
Vicrobot
  • 3,795
  • 1
  • 17
  • 31
2

The escape character \r (carriage return), means "start printing from beginning of this line". But some of operating systems use it as 'newline'. Following would work in Linux:

import time
import sys
#first text
print('Hey.', end="")
#flush stdout
sys.stdout.flush()
#wait a second
time.sleep(1)
#write a carriage return and new text
print('\rHow is your day?') 
pfabri
  • 885
  • 1
  • 9
  • 25
Adam Jenča
  • 582
  • 7
  • 20
  • 4
    It doesn't mean *start printing from the beginning of this line*. It's more like *return the cursor to the beginning of the current line*. Printing is not involved. Also, if the new string you want to print from the start of the line is shorter than the string already on the screen the excess chars from the old string will stay on the screen. I.e. your two string will be mixed together. – pfabri Feb 17 '21 at 10:34