seekable(): Check if a File Supports Seeking

seekable()
The `seekable()` method checks if a file object supports random access, meaning you can move the file pointer to a different location. It returns True if seeking is supported and False if not, such as for non-seekable objects like certain network streams.

Syntax

file_object.seekable()
Returns ( Boolean )True or False. If the file is seekable then it returns True. Otherwise False
If return value is True, then we can use seek() to move position of file object.

Example : Checking Seekability in Different Modes

fob=open('data.txt','r')
print(fob.seekable()) # True
fob.close()
fob=open('data.txt','w')
print(fob.seekable()) # True
fob.close()
fob=open('data.txt','+r')
print(fob.seekable()) # True
fob.close()

Example: Checking Non-Seekable Files

import sys
print(sys.stdin.seekable())  # False

Example: Using seek() After Verifying File is Seekable

fob = open('data.txt', 'r')
if fob.seekable():
    fob.seek(10)  # Move pointer to 10th byte
fob.close()
with open('data.txt', 'r') as f:
    print(f.seekable())  # Output: True

with open('data.txt', 'rb') as f:
    print(f.seekable())  # Output: True

Example 2: Non-seekable Streams

Demonstrates a non-seekable standard input stream.
import sys
print(sys.stdin.seekable())  # Output: False

Applications of seekable()

  • Random File Access: Check before using seek() to prevent errors in non-seekable files.
  • Stream Compatibility: Ensure data streams support random access, particularly for binary processing.

writable()

File Append File Write
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer