How to Clear Console in Python
Clearing the interpreter console is an easy task in Python and depending on the platform you are using, there are various commands you can use. You can also clear the interpreter programmatically.
Clear Console in Python
To clear the console in Python, use the system call. To clear the console programmatically, you need to use the os module and use the os.system() method.
System calls to clear the console:
- For the Linux system, use the clear command works.
- For the Windows system, use the cls command to clear the console.
The os is a built-in library that comes with Python3 installations.
I am using Mac, so I need to write Linux related command.
import os def clear_console(): os.system('clear') clear_console()
If you run the above command in your console then it will clear the screen.
For the Windows user, use the following code.
import os def clear_console(): os.system('cls') clear_console()
If you don’t want to use the full function then you can use the lambda function.
import os def clear_console(): return os.system('clear') clear_console()
And it will clear the console.
That’s pretty much it for clearing the console in Python.