File Methods
seekable()
Return True if the file stream supports random access positioning.
file.seekable()
file.seekable() checks whether the file object supports random access seek operations (like seek(), tell(), and truncate()).
Disk files are seekable, while streams, pipes, and sockets are generally not.
Result
Returns
True if seekable; otherwise False.
Run it
Try it
Run it and change it
import tempfile
with tempfile.NamedTemporaryFile(mode='w+', delete=True) as f:
print('Is disk file seekable:', f.seekable()) Output
Press Run to see what it prints.Worth knowing
- Error prevention: Calling
seek()on a non-seekable stream (likesys.stdin) raises anio.UnsupportedOperationexception.