Introduction
Python is the most widely used programming language in the world. If you are a Python developer or love to create various applications using Python, you end up working with Python interactive terminal/shell. Using the shell/terminal, you can write various commands to control your program. Several times you end up getting messy output in the terminal and want to clear the shell/terminal.
In this blog, we will learn different ways to clear a command shell/terminal screen in Python while running a Python script.
How to clear the Screen in Python?
In an interactive shell/terminal, we can simply use ctrl+l to clear the shell/terminal. There are several commands that can be used to clear screen in Python. Some of the commands are clear, cls, etc.
But what if we want to clear a command shell/terminal screen in Python while running a Python script? To do so, we need to write our own code/program. We will discuss the methods for the three operating systems macOS, Windows, and Linux.
Also see, Python Filter Function
Clearing Screen in Windows Operating System
Using the cls command
We can use cls command with the help of os.system() method to clear python terminal/shell in the windows Operating System.
Let’s understand how to clear screen in python using the cls command.
'''Importing os and time module'''
import os
import time
'''Printing coding ninjas five times'''
print('Coding Ninjas!\n'*5)
'''Sleep for 5 seconds after printing coding ninjas five times'''
time.sleep(5)
'''Clearing the Screen after 5 second halt '''
os.system('cls')
Output:
Initially-

After 5 seconds-

Clearing Screen in Linux/Unix Operating System
Using the clear command
We can use clear command with the help of os.system() method to clear python terminal/shell in the Linux/Unix operating system.
Let’s understand how to clear screen in python using the clear command.
'''Importing os and time module'''
import os
import time
'''Printing coding ninjas five times'''
print('Coding Ninjas!\n'*5)
'''Sleep for 5 seconds after printing coding ninjas five times'''
time.sleep(5)
'''Clearing the Screen after 5 second halt '''
os.system('clear')
Output:
Initially-

After 5 seconds-
Also see, Swapcase in Python, and Convert String to List Python
Clearing Screen when the Operating System is Unknown
If we don’t know what OS we are working on, in this case; first we need to determine the type of OS we are working on with the help of the code itself.
For Windows, the os name is "nt".
For Linux or mac, the OS name is "posix".
Let’s understand how to clear screen in python in an unknown OS.
Method 1: Using the os.system() method
'''Importing os and time module'''
import os
import time
'''We define our own clean function to clear the screen'''
def clean():
# For Windows
if os.name == 'nt':
_ = os.system('cls')
# For macOS and Linux
else:
_ = os.system('clear')
'''Printing coding ninjas five times'''
print('Coding Ninjas!\n'*5)
'''Sleep for 5 seconds after printing coding ninjas five times'''
time.sleep(5)
'''Clearing the Screen after 5 second halt '''
clean()
Output:
Initially-

After 5 seconds:

Method 2: Using the call method of the subprocess module
'''Importing os, subprocess, and time module'''
import subprocess, os
import time
'''We define our own clean function to clear the screen'''
def clean():
# For macOS and Linux
if os.name == 'posix':
_ = subprocess.call('clear')
# Windows
elif os.name == 'nt':
_ = subprocess.call('cls')
'''Printing coding ninjas five times'''
print('Coding Ninjas!\n'*5)
'''Sleep for 5 seconds after printing coding ninjas five times'''
time.sleep(5)
'''Clearing the Screen after 5 second halt '''
clean()
Output:
Initially-

After 5 seconds-
