Python file.isatty() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
file method

Definition

isatty() tells you whether a stream is an interactive terminal (a TTY), the kind a person types into and reads from.

Regular files on disk and in-memory buffers always return False. Programs use it to add colors or live progress only when a human is watching, and plain text when output is saved to a file.

file.isatty() is a method of the file objects that the built-in open() returns, so no import is needed. It works on text files (TextIOWrapper), binary files (BufferedReader, BufferedWriter, BufferedRandom), and raw files (FileIO, opened with buffering=0).

Syntax

Call it with no arguments on any open stream.

python
if stream.isatty():
    print("interactive")
CallWhat it does
open(path, "w", encoding="utf-8").isatty()False for a regular file.
io.StringIO().isatty()False for an in-memory buffer.
sys.stdout.isatty()True in a terminal, False when output is redirected.

sys.stdout.isatty() changes with how the program is started, so its result is not shown in the examples below.

Method signatures
python
file.isatty()   # -> bool
# os.isatty(f.fileno()) gives the same answer for real files

Parameters

isatty() takes no parameters.

Return Value

Return ValueDescription
TrueThe stream is attached to a terminal device.
FalseA regular file, a pipe, a redirected stream, or an in-memory buffer.

Python Examples

Each example works inside a temporary folder that is deleted at the end, so nothing is left on your disk.

Example 1 — Confirm a File on Disk Is Not a Terminal

Ask the text, binary, and raw objects for the same notes file, and compare with os.isatty().

python
import os
import tempfile

with tempfile.TemporaryDirectory() as folder:
    path = os.path.join(folder, "notes.md")
    with open(path, "w", encoding="utf-8") as f:
        print("text:", f.isatty())
    with open(path, "rb") as f:
        print("binary:", f.isatty())
    with open(path, "rb", buffering=0) as f:
        print("raw:", f.isatty(), os.isatty(f.fileno()))

How It Works

  1. Disk files are not TTYs. Whatever layer you ask, a regular file reports False.
  2. Answer comes from the bottom. The text and buffered objects pass the question down to the raw file.
  3. Descriptor version. os.isatty(f.fileno()) gives the same answer using the raw OS number.

Example 2 — Add Terminal Colors Only When a Person Is Watching

A helper wraps text in ANSI bold codes for terminals but writes plain text to a log file.

python
import os
import tempfile

BOLD, RESET = "\033[1m", "\033[0m"


def styled(text, stream):
    if stream.isatty():
        return BOLD + text + RESET
    return text


with tempfile.TemporaryDirectory() as folder:
    path = os.path.join(folder, "build.log")
    with open(path, "w", encoding="utf-8") as log:
        log.write(styled("build passed", log) + "\n")
    with open(path, encoding="utf-8") as log:
        print(repr(log.read()))

How It Works

  1. Escape codes. "\033[1m" turns bold on in most terminals and "\033[0m" resets it.
  2. Ask the destination. styled() checks the stream it will write to, not the screen.
  3. Clean log. The log file is not a terminal, so no escape codes end up in it, as repr() confirms.

Example 3 — Choose a Progress Format for Logs vs Terminals

Terminals get a single updating line; anything else gets one finished line per step. Test with an in-memory stream.

python
import io


def report(stream, done, total):
    if stream.isatty():
        stream.write(f"\r{done}/{total}")
    else:
        stream.write(f"{done}/{total} finished\n")


capture = io.StringIO()
for done in (1, 2, 3):
    report(capture, done, 3)
print(capture.isatty())
print(capture.getvalue().splitlines())

How It Works

  1. Two styles. On a terminal, "\r" returns the cursor to the start of the line so the counter updates in place.
  2. Testable without a terminal. io.StringIO is never a TTY, so it records the log-friendly format.
  3. One line per step. splitlines() shows three complete messages, easy to grep later.

Frequently Asked Questions

False. A regular file on disk is never an interactive terminal, whatever mode it was opened in.
Use sys.stdout.isatty(). It is True when printing to a console and False when output is redirected to a file or pipe.
TTY is short for teletypewriter. Today it means an interactive terminal device, such as a console window.
f.isatty() is a method of the file object. os.isatty(fd) takes a raw file descriptor, such as the value of f.fileno(). They agree for real files.
ANSI color codes look like garbage in log files. Checking isatty() lets you color terminal output and keep files plain.

Next: file.peek()

Look at upcoming bytes in a binary buffered reader without moving the position.

peek() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful