Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to clear your printed text in python?

Tags:

python

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.

like image 663
Joel Madsen Avatar asked Oct 25 '13 18:10

Joel Madsen


People also ask

How do you clear the print in Python?

You can simply “cls” to clear the screen in windows.

How do you clear a text file in Python?

Clear a Text File Using the open() Function in write Mode Opening a file in write mode clears its data. Also, if the file specified doesn't exist, Python will create a new one. The simplest way to delete a file is to use open() and assign it to a new variable in write mode.

How do you clear all text in console Python?

You can clear the Python interpreter console screen using a system call. System calls to clear the console: For the window system, cls clear the console. For the Linux system, clear command works.

How do you clear inputs in Python?

The clear() method removes all elements in a set.


1 Answers

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
like image 151
alper Avatar answered Oct 19 '22 17:10

alper