readable() returns True when you may call reading methods such as read() and readline() on the stream, and False when they would fail.
For files the answer comes from the mode given to open(): modes with "r" or "+" are readable; plain "w", "a", and "x" are not.
file.readable() 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 an open stream.
python
if f.readable():
data = f.read()
Call
What it does
open(path, "r", ...).readable()
True.
open(path, "w", ...).readable()
False: write-only.
open(path, "a+", ...).readable()
True: the "+" adds reading.
It only checks permission, not content: an empty readable file still returns True.
Method signatures
python
file.readable() # -> bool
# IOBase.readable() returns False unless a subclass overrides it
Parameters
readable() takes no parameters.
Return Value
Return Value
Description
True
Reading methods are allowed.
False
Reading would raise io.UnsupportedOperation.
Python Examples
Each example works inside a temporary folder that is deleted at the end, so nothing is left on your disk.
Example 1 — List readable() for Five Open Modes
Open one plan file with w, r, a, r+, and a+ and print the answer for each.
python
import os
import tempfile
with tempfile.TemporaryDirectory() as folder:
path = os.path.join(folder, "plan.txt")
for mode in ("w", "r", "a", "r+", "a+"):
with open(path, mode, encoding="utf-8") as f:
print(f"{mode:<3}", f.readable())
Output
w False
r True
a False
r+ True
a+ True
How It Works
Create first."w" comes first in the loop, so the file exists before "r" tries to open it.
Write-only modes."w" and "a" report False: you cannot read through them.
Plus adds the other direction."r+" and "a+" can both read and write, so they report True.
Aligned column. The format spec :<3 pads each mode to three characters for a neat table.
Example 2 — Guard a Loader Against Write-Only Streams
A load() helper returns a message instead of crashing when the stream cannot be read.
python
import io
import os
import tempfile
def load(stream):
if not stream.readable():
return "cannot read: stream is write-only"
return stream.read()
with tempfile.TemporaryDirectory() as folder:
path = os.path.join(folder, "motd.txt")
with open(path, "w", encoding="utf-8") as f:
f.write("Welcome back!\n")
print(load(f))
with open(path, encoding="utf-8") as f:
print(repr(load(f)))
print(repr(load(io.StringIO("in memory"))))
Output
cannot read: stream is write-only
'Welcome back!\n'
'in memory'
How It Works
Check, then act.load() asks readable() before reading, so a write handle gets a friendly message.
Read handle. The same function returns the full message of the day from a file opened for reading.
Any stream works.io.StringIO is readable too, so the helper accepts in-memory text without changes.
Example 3 — Inspect the Error from Reading a Write-Only File
Try read() on a binary upload file opened with "wb" and look at the exception type.
python
import io
import os
import tempfile
with tempfile.TemporaryDirectory() as folder:
path = os.path.join(folder, "upload.bin")
with open(path, "wb") as f:
print(f.readable())
try:
f.read()
except OSError as error:
print(type(error).__name__, isinstance(error, ValueError))
print(issubclass(io.UnsupportedOperation, OSError))
Output
False
UnsupportedOperation True
True
How It Works
Write-only binary. A file opened with "wb" is a BufferedWriter, and readable() says False.
Caught as OSError. The failed read raises io.UnsupportedOperation, which an except OSError clause catches.
Two parents. The same exception is also a ValueError, so older code catching either one keeps working.
Frequently Asked Questions
False. Mode "w" is write-only. Use "w+" or "r+" if you need to read and write the same file object.
"r" (the default), "r+", "w+", "a+", "x+", and their binary versions such as "rb" and "r+b".
io.UnsupportedOperation, a subclass of both OSError and ValueError, with a message such as "not readable".
No. It only says whether reading is allowed. An empty file opened with "r" is readable, and read() simply returns "".
It raises ValueError instead of returning False. Check f.closed first if the file might be closed.