Online Tutorials & Training Materials |
Register Login

Clear the Screen in Python

|| 0

Clear the Screen in Python

Sometimes while working with programs on the screen or with their output, we want to end the messy screen, and then we need to clear the screen. But there is no such function to use because a clear screen is not as clear we understand, it is varies according to an operating system and how the Python is supported (terminal window, GUI, windowing environment or on the browser) these are using a different mechanism with distinct ways to clear screen.

In an interactive shell session, we can use simply Ctrl+l.

While running a python script, we need to clear the screen , but there is no method  or a function to clear screen, then we can use an ANSI escape sequence.

For Unix/Linux:

import os
os.system ('clear')

For Windows (I am not sure though, it's been ages since I last used windows):

import os
os.system ('cls')


Related Articles