Python

How to clear the console in Python – (terminal)

January 2, 2022

How to clear the console in Python – (terminal)

In this article you will learn how to clear the console in Python, also called terminal, in a simple and effective way.

clear the console in Python cover

Hey you programmer, what’s up? Let’s learn more about Python!

Cleaning up the terminal or console with just Python is a very simple task

We’ll just need to import the lib os, and use the system method on it.

That’s just  to have access to some commands

Here’s the practical example on how to clean the console:

import os

os.system('cls' if os.name == 'nt' else 'clear')

With the os.system instruction above, you can clean the terminal

And realize that we are using an approach that will work on both Windows and Linux

Because the code checks for cls, command that cleans the terminal on Windows

If not, it will use the clear,a command that clears the Linux console

So your code will work on both operating systems.

By shortcut

If you are not looking for an alternative via programming, you can write cls or clear yourself in the terminal

Or also use the Ctrl + L hotkey

These two ways are not via programming, but work in the same way as code.

Conclusion

In this article we learned a very simple and effective way to clear the console in Python.

We are also checking which operating system is being used, to be able to support any computer

Through lib OS, we were able to achieve our goal

Running  a cls or clear, depending on the user’s OS

Want to learn more about Python? Click here!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x