Search code examples
pythonstdouttty

I can't sys.stdout.seek


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?


Solution

  • 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.