Online Tutorials & Training Materials | STechies.com
Register Login

Python Clear Screen

13 Jun 2020 7:47 am || 1000

When programming in Python using the interactive shell, you can see the output after running every script. This can make the screen a bit clumsy and you might have problems locating a particular output. In that case, you might want to clear the screen.     

You might want to clear the screen while executing a python script. Unfortunately, there’s no built-in keyword or function/method to do so. So, you have to do it on your own.

How to clear the shell screen in Python?    

We will look here following ways to clear the Python output screen in this article:

1) Using ANSI Escape

You can use ANSI escape sequences. But these are not portable and may not produce the output you are looking for.

The most common ways to clear an interactive shell screen on Windows and Linux platforms are given below –   

For Windows terminals:

> cls

For Linux / OS X platforms:

> clear

If you want to clear a screen on other programming environments other than Python, such as Bash, MySQL or MATLAB, just press CTRL + L.

2) Using the Subprocess Module

You can use the subprocess module in Python to clear the output screen.  

Windows:

import subprocess as sp

sp.call('cls',shell=True)

The second line of the code must be replaced with tmp = sp.call('cls',shell=True), to prevent 0 from popping up at the top of the output screen.   

Linux:

import subprocess as sp

sp.call('clear',shell=True)

Replace the second line of the code with tmp = sp.call('clear',shell=True) for the reason mentioned earlier.  

3) Using the OS Module

Use the import statement to load the os library. Then use the system method and pass ‘cls’ as the parameter to clear the output screen.

Windows:

import os

os.system('cls')

Linux/ OS X:

import os

os.system('clear')

4) Using Click Module

The click module works on Windows, Linux and OS X platforms.

Windows:

import click

click.clear()

5) Custom Program to Clear Screen

You can write a Python program to clear the screen –

# The code works on all platforms such as Windows, Linux and OS-X
import platform    # For obtaining the operating system information
import os          # Import the os module to clear screen
def clr_scr():
    #Python program to clear screen
    #Get command to execute
    if(platform.system().lower()=="windows"):
        cmd='cls'
    else:
        cmd='clear'   
    # Run command
    os.system(cmd)
# Call function to clear screen
clr_scr()

6) Using Clear-Screen Class

from clear_screen import clear
# Python program to clear screen
# Using clear Module
# Import clear_screen Class from clear Module
from clear_screen import clear
# execute function clear()
clear()

Conclusion

As discussed above, there are multiple ways through which you can clear the screen in Python. It also depends on the module you have installed.

If you are running Python scripts on multiple platforms, then you can use the clear command, click module or can just create your own program to clear the interactive shell screen. Make sure you have properly loaded modules using the import statement before using them.   


Comments


View previous comments