How to Clear the Screen in Python (Windows, Mac, Linux)

Here’s how to clear the screen in Python: call the terminal’s own clear command with os.system(), using cls on Windows and clear on macOS and Linux. One line handles both:

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

The screenshots show Command Prompt windows on Windows 11 with Python 3.12.5, captured after the clear ran, so you only see what survived it.

I’ll cover the os.system approach, a reusable function, ANSI escape codes, redrawing a single line, and why clearing doesn’t work in IDLE or when output is redirected.

How to clear the screen in Python with os.system

Python has no built-in clear command, so you ask the operating system. os.name is "nt" on Windows, which tells you which command to run:

import os
import time

print("Loading sales report...")
print("Connecting to database...")
time.sleep(2)

os.system("cls" if os.name == "nt" else "clear")    # the screen is wiped here
print("Sales report for October 2026")
print("Austin   $12,500.50")
print("Denver    $9,800.25")

Output:

Sales report for October 2026
Austin   $12,500.50
Denver    $9,800.25
Command Prompt window showing only a sales report after a Python script cleared the earlier loading messages with os.system cls
The two loading lines and the command you typed are gone; only the report printed after the clear remains.

Text printed before the clear disappears, and text after it starts at the top. For running scripts like this, see how to run a Python script.

A reusable clear_screen() function

Wrap the logic in a function so the rest of your program just calls clear_screen(). Using subprocess.run() avoids some quirks of os.system:

import os
import shutil
import subprocess
import time

def clear_screen():
    """Clear the terminal on Windows, macOS and Linux."""
    if os.name == "nt":
        subprocess.run("cls", shell=True)
    else:
        subprocess.run(["clear"])

for seconds in range(3, 0, -1):
    clear_screen()
    print(f"Next update in {seconds}...")
    time.sleep(1)
clear_screen()
print("Updated at 10:30 AM")
print("terminal width:", shutil.get_terminal_size().columns, "columns")

Output:

Updated at 10:30 AM
terminal width: 80 columns
Command Prompt showing the final screen of a Python countdown that cleared the terminal each second with a clear_screen function
Each countdown step cleared the previous one, so only the final update is left on screen.

Clearing in a loop like this is how simple terminal dashboards work. The countdown uses a reverse range; see for i in range in reverse.

Clearing the screen with ANSI escape codes

Most modern terminals, including Windows Terminal and Command Prompt on Windows 10 and 11, understand ANSI codes. \033[2J clears the screen and \033[H moves the cursor home:

import time

print("Old output line 1")
print("Old output line 2")
time.sleep(2)

print("\033[2J\033[H", end="")         # ANSI: clear the screen, move cursor to top-left
print("Fresh screen via ANSI codes")

for pct in range(0, 101, 25):
    print(f"\rDownloading... {pct:3d}%", end="", flush=True)   # \r redraws one line
    time.sleep(0.3)
print()

Output:

Fresh screen via ANSI codes

Downloading... 100%
Command Prompt after Python ANSI escape codes cleared the screen, followed by a single-line progress percentage updated with carriage return
The ANSI codes cleared the old lines without starting a new process. The progress line redraws itself with \r.

Often you don’t need to clear the whole screen at all. Printing \r with end="" rewrites just the current line, which is perfect for progress counters.

Why doesn’t clearing the screen work in IDLE?

IDLE’s shell, PyCharm’s Run window and VS Code’s Output panel aren’t real terminals, so cls and ANSI codes do nothing useful there. Run the script in Command Prompt, PowerShell or Terminal instead.

The same happens when output is redirected to a file or another program. Check sys.stdout.isatty() and skip the clear when it’s false:

import os
import sys

if sys.stdout.isatty():
    os.system("cls" if os.name == "nt" else "clear")
    print("cleared the terminal")
else:
    print("not a terminal (output is redirected), so there is nothing to clear")

Output:

not a terminal (output is redirected), so there is nothing to clear

When I ran this from my build tool, output was captured instead of shown in a terminal, so it took the second branch. For more on running system commands from Python, see running Python scripts.

Ways to clear the screen in Python

MethodWorks inNotes
os.system("cls") / os.system("clear")Real terminalsSimplest; starts a small process
subprocess.run(...)Real terminalsSame idea, more control
print("\033[2J\033[H")ANSI-capable terminalsNo new process; fast in loops
print("\r...", end="")Most terminalsRedraws one line only
Restart the shellIDLE (Ctrl+F6)IDLE has no real clear

A few more terminal and scripting guides:

Frequently asked questions

How do I clear the screen in Python?

Use os.system("cls" if os.name == "nt" else "clear"). It runs the right command for Windows, macOS or Linux.

Why does os.system(‘cls’) print 0?

In the interactive interpreter, os.system() returns the command’s exit code, which is echoed. Assign it to a variable, like _ = os.system("cls").

How do I clear the screen in IDLE?

You can’t with code, because IDLE isn’t a real terminal. Restart the shell with Ctrl+F6, or run the script in a terminal.

How do I clear just one line?

Print a carriage return and overwrite it: print("\rnew text", end="", flush=True).

Do ANSI escape codes work on Windows?

Yes, in Windows Terminal and in Command Prompt on Windows 10 and 11.

Where is os.system documented?

See os.system in the Python docs.