2

From what I know, sys.stdout is a file that represents the stdout of a terminal. However, when I try to use sys.stdout.seek, whatever parameters I give it, it throws an error:

IOError: [Errno 29] Illegal seek

What's going on? Is it the fact that I'm using the TTY itself and not a virtual terminal like xterm? How can I resolve this?

BoxTechy
  • 339
  • 2
  • 6
  • 16
  • Oh, you must be using Python 2. Python 3.6's error looks like this: `io.UnsupportedOperation: underlying stream is not seekable`. – Right leg Jul 27 '17 at 15:22
  • 3
    You can only seek in a regular file. What exactly are you expecting to happen when seeking in the TTY? – Sven Marnach Jul 27 '17 at 15:23
  • It is file-like, but not actually a file – Ofer Sadan Jul 27 '17 at 15:23
  • @SvenMarnach How is stdout different from a regular file? (genuine question) – Right leg Jul 27 '17 at 15:24
  • 4
    @Rightleg stdout _might_ be a regular file. It might also be a pipe or a tty or some other file-like object. So the actual question is how a TTY or a pipe is different from a regular file. And one of the differences is that you can't seek in a pipe or a TTY since it's only an ephemeral stream of data. You can't go back and forth in that stream; it's not persisted anywhere, in contrast to a regular file that is stored on disk. – Sven Marnach Jul 27 '17 at 15:27
  • @SvenMarnach Thanks for the enlightment. – Right leg Jul 27 '17 at 15:28
  • [`sys.stdout.isatty()`](http://images.wisegeek.com/teletypewriter.jpg) – wim Jan 24 '18 at 19:58

1 Answers1

3

When stdout is a TTY it's a character device and it's not seekable, you cannot seek in a TTY. Same for tell, it's not implemented:

>>> sys.stdout.tell()
IOError: [Errno 29] Illegal seek

However, when you redirect standard streams to a file for example in a shell:

$ my program > ouput.txt

Now you can seek from stdout, stdout now is a file.

Please look at this reference if you're working with Unix:

Understanding character device (or character special) files.

GIZ
  • 4,409
  • 1
  • 24
  • 43