0

I am running on Python 3.7.1 and I've been trying to find a way to clear a screen of any previously printed messages. The problem is that os.system("cls") does nothing, it only makes a small window pop up for a fraction of a second, then it closes. I've tried to add a \n at the end and multiplying it by how many letters there are, still not working.

braX
  • 11,506
  • 5
  • 20
  • 33

2 Answers2

0

Unfortunately, there’s no built-in keyword or function/method to clear the screen. So, we do it on our own.

We can use ANSI escape sequence but these are not portable and might not produce desired output.

# import only system from os 
from os import system, name 

# import sleep to show output for some time period 
from time import sleep 

# define our clear function 
def clear(): 

    # for windows 
    if name == 'nt': 
        _ = system('cls') 

    # for mac and linux(here, os.name is 'posix') 
    else: 
        _ = system('clear') 

# print out some text 
print('hello geeks\n'*10) 

# sleep for 2 seconds after printing output 
sleep(2) 

# now call function we defined above 
clear() 
techdoodle
  • 85
  • 6
-1

I don't think that there is a way, or at least have never seen one. However, there is a workaround which would look like

print "\n" * 100.

This will simply print 100 newlines, which will clear the screen for you.

You could also put it in a function

def cls(): print "\n" * 100

And then when you need it just call it with cls()

ech0
  • 512
  • 4
  • 14