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.
You can simply “cls” to clear the screen in windows.
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.
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.
The clear() method removes all elements in a set.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With