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)
Call
What 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 Value
Description
True
seek(), tell(), and truncate() are supported.
False
The 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())
Three object types.open() returns a TextIOWrapper, a BufferedReader, or a raw FileIO depending on mode and buffering.
Same answer. All three sit on the same regular file on disk, so all three report True.
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))
Output
True (b'job-1\n', 3)
False (b'msg-a\n', 2)
How It Works
A pretend pipe.OneWay only implements readable() and readinto(); the inherited seekable() returns False.
Wrapped like a real file.io.BufferedReader(OneWay(...)) behaves like open(..., "rb") but asks the raw stream whether it can seek.
Copy when needed. For the non-seekable stream the helper reads everything into an io.BytesIO, which can rewind.
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())
Output
True 0
['old entry\n', 'new entry\n']
How It Works
Seekable stream. The log is a regular file, so seekable() is True and seek(0) returns 0.
Append wins. In "a" mode the operating system moves every write to the end of the file, whatever the position.
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.