Python file.readable() Method

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

Definition

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()
CallWhat 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 ValueDescription
TrueReading methods are allowed.
FalseReading 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())

How It Works

  1. Create first. "w" comes first in the loop, so the file exists before "r" tries to open it.
  2. Write-only modes. "w" and "a" report False: you cannot read through them.
  3. Plus adds the other direction. "r+" and "a+" can both read and write, so they report True.
  4. 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"))))

How It Works

  1. Check, then act. load() asks readable() before reading, so a write handle gets a friendly message.
  2. Read handle. The same function returns the full message of the day from a file opened for reading.
  3. 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))

How It Works

  1. Write-only binary. A file opened with "wb" is a BufferedWriter, and readable() says False.
  2. Caught as OSError. The failed read raises io.UnsupportedOperation, which an except OSError clause catches.
  3. 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.

Next: file.read1()

Return up to size bytes using at most one read of the underlying raw stream.

read1() →

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.

16 people found this page helpful