How to clear screen in python terminal

When working with the python interactive shell, you may end up having a cluttered screen. For Windows and Linux users, one can run cls or clear to clear their screens. But this doesn't seem to cut it while working in the python shell. Here are a few quick tips to achieve this in python.

We need to get into python shell first 😆. To do this, type python in your terminal or if you want to access the Django DB shell, python manage.py shell will suffice.

first method

The simplest method is using ctrl+l for Linux/OS X terminals.

second method

Another handy way is by using the os module that ships with the standard python library. This module provides a portable way of using operating system dependent functionality.

from os import system

clear = lambda: system('clear')

We are utilising python's lambda functions to achieve this functionality. By calling on the underlying os clear function for UNIX based system and assigning that to our newly created clear function.

This way if we want to clear the screen we can call the function like so clear()

for windows systems, the following would work just as well.

from os import system

cls = lambda: system('cls')

and call the function in the python shell by running cls()

Note that you don't have to declare the function as clear or cls. You can name them whatever you want, as long as you use that name when calling them.

Other useful tricks you can use with the os module

Getting the current working directory

import os
os.getcwd()

In my case, this returned '/Users/me/Projects'

Changing the current working directory

os.chdir('..')

This should work similar to cd .. returning '/Users/me'

listing items in a directory

os.listdir('directory_path')

similar to the ls command.

get currently logged in user operating the terminal

os.getlogin()

For more information on the os module, check out the documentation

Thanks for reading and feel free to contact me if you have any questions on twitter.

Sponsors

Please note that some of the links below are affiliate links and at no additional cost to you. Know that I only recommend products, tools and learning services I've personally used and believe are genuinely helpful. Most of all, I would never advocate for buying something you can't afford or that you aren't ready to implement.

Scraper API

Scraper API is a startup specializing in strategies that'll ease the worry of your IP address from being blocked while web scraping.They utilize IP rotation so you can avoid detection. Boasting over 20 million IP addresses and unlimited bandwidth.

In addition to this, they provide CAPTCHA handling for you as well as enabling a headless browser so that you'll appear to be a real user and not get detected as a web scraper. Usage is not limited to scrapy but works with requests, BeautifulSoup and selenium in the python ecosystem. Integration with other popular platforms such as node.js, bash, PHP and ruby is also supported. All you have to do is concatenate your target URL with their API endpoint on the HTTP get request then proceed as you normally would on any web scraper. Don't know how to webscrape? Don't worry, I've covered that topic extensively on the webscraping series. All entirely free!

scraperapi

Using this scraper api link and the promo code lewis10, you'll get a 10% discount on your first purchase!! You can always start on their generous free plan and upgrade when the need arises.

Digital Ocean

Looking for cloud hosting for your projects? Digital Ocean is just the right partner for you.

They make it simple to launch in the cloud and scale up as you grow – with predictable pricing, team accounts, and more. Their intuitive control panel and API give you time to build more and spend less time managing your infrastructure.

Using this digitalocean link to sign up, you'll get $100 in credit for the first two months!

Share to social media