Sometimes, we want to clear the interpreter console with Python.
In this article, we’ll look at how to clear the interpreter console with Python.
How to clear the interpreter console with Python?
To clear the interpreter console with Python, we can run the cls command if the script is run on Windows and clear otherwise.
We can do this with the os.system method.
For instance, we write:
import os
def cls():
os.system('cls' if os.name=='nt' else 'clear')
cls()
We have the cls function that uses os.name to check for the platform that it’s running.
If os.name returns 'nt' then it’s running on Windows.
We use os.system to run cls or clear depending on the platform.
Conclusion
To clear the interpreter console with Python, we can run the cls command if the script is run on Windows and clear otherwise.
We can do this with the os.system method.