I can't sys.stdout.seek

淺唱寂寞╮ 提交于 2019-12-24 00:42:31

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/45354873/i-cant-sys-stdout-seek

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!