I ran "pydoc file.seek" and this line from documentation puzzled me. "Note that not all file objects are seekable." As far as I understand, "not seekable" means "you can't use seek, even if you have access permission". I don't understand, how is it even possible? How can there be a file objects that can't be seekable?
Asked
Active
Viewed 595 times
0
-
1"file object" does not necessarily mean an actual disk file. Maybe it's a socket, or a pipe, or any other thing that's convenient to treat _as though it were a file_ when it isn't actually a file. – John Gordon Apr 05 '19 at 05:56
-
@JohnGordon Ah, then I understand. – KarmaPeasant Apr 05 '19 at 05:57
-
Seekable only if the file stream supports random access. Any deviation may encounter this – Srini V Apr 05 '19 at 05:58
-
@SriniV Wait, are you saying that a file on a tape wouldn't be seekable? – KarmaPeasant Apr 05 '19 at 05:59
1 Answers
1
A file on disk is always seekable, but the file handle abstraction does not apply only to files on a local disk. Unidirectional information streams like pipes, network sockets etc are only there for you as long as you keep them buffered or in memory; unless you have saved the information yourself, you cannot go back and refetch a client's response to you from five minutes ago.

tripleee
- 175,061
- 34
- 275
- 318
-
What if I access file of another computer (let's call it server) over network? Like Youtube's videoplayer seem to be able perform random access to a videofile that it plays, making it possible to go back and forth, even if it's not buffered. Not without a lag, but it seems that random access over network is possible. – KarmaPeasant Apr 05 '19 at 06:07
-
1That's because there is a more elaborate mechanic at play than a single unidirectional network connection. Without examining the player, I'd assume there is a protocol for telling the server you want to rewind the stream. That doesn't change the fact that the bits you have played or skipped are no longer available unless you get the server to retransmit them. (Of course it's also possible at least in theory that the client could keep the played or skipped bits in memory so it can go back; but again, the network connection is not seekable.) – tripleee Apr 05 '19 at 06:10