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")
Call
What 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 Value
Description
True
The stream is attached to a terminal device.
False
A 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()))
Output
text: False
binary: False
raw: False False
How It Works
Disk files are not TTYs. Whatever layer you ask, a regular file reports False.
Answer comes from the bottom. The text and buffered objects pass the question down to the raw file.
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()))
Output
'build passed\n'
How It Works
Escape codes."\033[1m" turns bold on in most terminals and "\033[0m" resets it.
Ask the destination.styled() checks the stream it will write to, not the screen.
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())