Python file.seekable() Method

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

Definition

seekable() answers one question: can this stream move its position? It returns True for regular files on disk and in-memory streams, and False for one-way streams such as pipes, sockets, and many terminals.

When it is False, seek(), tell(), and truncate() raise OSError. Check it before code that needs to rewind or jump around.

file.seekable() 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 f.seekable():
    f.seek(0)
CallWhat it does
open(path, "rb").seekable()True for a regular file.
io.BytesIO().seekable()True for in-memory streams.
pipe.seekable()False for a pipe or a one-way stream.

Being seekable is about the stream, not the mode: a file opened with "a" is seekable, yet every write still goes to the end.

Method signatures
python
file.seekable()   # -> bool
# IOBase.seekable() returns False unless a subclass overrides it

Parameters

seekable() takes no parameters.

Return Value

Return ValueDescription
Trueseek(), tell(), and truncate() are supported.
FalseThe stream can only move forward; those methods raise OSError.

Python Examples

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

Example 1 — Check seekable() on Text, Buffered, and Raw Files

Open one inventory file three ways and ask each object whether it can seek.

python
import os
import tempfile

with tempfile.TemporaryDirectory() as folder:
    path = os.path.join(folder, "inventory.txt")
    with open(path, "w", encoding="utf-8") as f:
        print(type(f).__name__, f.seekable())
    with open(path, "rb") as f:
        print(type(f).__name__, f.seekable())
    with open(path, "rb", buffering=0) as f:
        print(type(f).__name__, f.seekable())

How It Works

  1. Three object types. open() returns a TextIOWrapper, a BufferedReader, or a raw FileIO depending on mode and buffering.
  2. Same answer. All three sit on the same regular file on disk, so all three report True.
  3. Type names only. type(f).__name__ prints just the class name, never a path.

Example 2 — Handle a Stream That Cannot Rewind

A helper counts lines and then rewinds; for a one-way stream it first copies the data into memory.

python
import io
import os
import tempfile


class OneWay(io.RawIOBase):
    """A read-only stream that cannot rewind, like a pipe or a socket."""

    def __init__(self, data):
        super().__init__()
        self.data = data

    def readable(self):
        return True

    def readinto(self, buffer):
        n = min(len(buffer), len(self.data))
        buffer[:n] = self.data[:n]
        self.data = self.data[n:]
        return n


def first_line_and_count(stream):
    if not stream.seekable():
        stream = io.BytesIO(stream.read())
    count = len(stream.readlines())
    stream.seek(0)
    return stream.readline(), count


with tempfile.TemporaryDirectory() as folder:
    path = os.path.join(folder, "queue.txt")
    with open(path, "wb") as f:
        f.write(b"job-1\njob-2\njob-3\n")
    with open(path, "rb") as f:
        print(f.seekable(), first_line_and_count(f))

pipe = io.BufferedReader(OneWay(b"msg-a\nmsg-b\n"))
print(pipe.seekable(), first_line_and_count(pipe))

How It Works

  1. A pretend pipe. OneWay only implements readable() and readinto(); the inherited seekable() returns False.
  2. Wrapped like a real file. io.BufferedReader(OneWay(...)) behaves like open(..., "rb") but asks the raw stream whether it can seek.
  3. Copy when needed. For the non-seekable stream the helper reads everything into an io.BytesIO, which can rewind.
  4. Same result shape. Both calls return the first line and the line count, so the caller never has to care which kind of stream it passed.

Example 3 — Seek in Append Mode and Still Write at the End

An audit log opened with "a" is seekable, but seeking to the start does not change where writes go.

python
import os
import tempfile

with tempfile.TemporaryDirectory() as folder:
    path = os.path.join(folder, "audit.log")
    with open(path, "w", encoding="utf-8") as f:
        f.write("old entry\n")

    with open(path, "a", encoding="utf-8") as f:
        print(f.seekable(), f.seek(0))
        f.write("new entry\n")

    with open(path, encoding="utf-8") as f:
        print(f.readlines())

How It Works

  1. Seekable stream. The log is a regular file, so seekable() is True and seek(0) returns 0.
  2. Append wins. In "a" mode the operating system moves every write to the end of the file, whatever the position.
  3. Order preserved. Reading back shows the new entry after the old one, so earlier records are never overwritten.

Frequently Asked Questions

It returns True if the stream supports seek(), tell(), and truncate(). Regular files and in-memory streams are seekable; pipes and sockets are not.
It depends on how the program runs. Redirected from a file it usually is; from a pipe or an interactive terminal it usually is not. Always check instead of assuming.
It raises an OSError subclass, typically io.UnsupportedOperation. Read the data into io.BytesIO first if you need to rewind.
seekable() describes the stream. Append mode is an extra rule from the operating system that moves every write to the end of the file.
Not by default: IOBase.seekable() returns False. Override seekable(), seek(), and tell() to support random access.

Next: file.tell()

Return the current position: a byte offset in binary mode, an opaque cookie in text mode.

tell() →

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.

15 people found this page helpful