How to clear the console with Python

When one writes and practices a lot of code in their Python interactive shell, their console gets dirty, so it is necessary to clean it often. There are many ways to accomplish a task with the Python computer programming language, like with any computer technology out there, but in here I will explain only a few techniques to help the Python beginner to remove the dirt from their Python interactive console.

Having my Python interactive shell full of code, dirty to the point that it is necessary for me to clean it, first of all, I will import a module which helps to execute system commands. The name of the module is os, it can be used by the Python coder to run different shell commands part of the operating system. Not only that, the Python os library offers other builtin functionalities related to the operating system.

First, use the Python import statement like shown below, with the main purpose of importing the utilities offered by the Python os package.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

import os

For one to execute a shell command part of the operating system with the help of the os module, they have to follow the syntax which is being shown below.

os.system(cmd)

The syntax for the os module being shown above, is very clear to anyone who has a little knowledge on the Python computer programming language. Everything one needs to execute a shell command on their operating system is the utility shown above, and the command.

For the purpose of this short tutorial, I am going to explain to the Python beginners how they can make use of the utilities being shown below to clear their interactive consoles.

os.system(‘cls’) # for windows users
os.system(‘clear’) # for unix users

Final thoughts

The os module is very useful to the Python developer. There are many utilities that come with it, but the one you need to run shell commands specific to the operating system you’re working with, is the os.system tool.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Leave a Reply

Your email address will not be published. Required fields are marked *

*