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

Some of the links below are affiliate links, which means I may earn a small commission — at no extra cost to you. I only recommend tools, products, and services I’ve personally used and found genuinely helpful in my work or learning journey. These sponsors quietly help keep the lights on around here — and I’m grateful for that. Please don’t buy anything you can’t afford or aren’t ready to put to use.

Scraper API

Scraper API is built for developers who need reliable, scalable web scraping without the headache of IP blocks or CAPTCHA walls. They handle IP rotation, CAPTCHA solving, and even headless browser emulation — so your scrapers look and behave like real users.

With over 20 million IPs, unlimited bandwidth, and built-in support for popular libraries like requests, BeautifulSoup, Scrapy, and Selenium, it works seamlessly across the Python ecosystem — and beyond. Whether you're using Node.js, Bash, PHP, or Ruby, integration is as simple as appending your target URL to their API endpoint.

If you're new to scraping, don’t worry — I’ve covered it extensively in the webscraping series, and it's all free.

Use this link + code lewis10 to get 10% off your first purchase. They’ve got a generous free plan — scale up only when you need to.

scraperapi

Digital Ocean

DigitalOcean makes it easy to launch, scale, and manage your cloud infrastructure — without the overwhelm.

Simple pricing. Powerful API. A clean UI that actually respects your time.

Use this link to get $200 in free credits for your first two months. Perfect for testing, launching, or scaling your next side project or startup.

Share to social media