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.
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.
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
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.