Skip to main content
 首页 » 编程设计

python之我不能 sys.stdout.seek

2024年10月01日6langtianya

据我所知,sys.stdout 是一个表示终端标准输出的文件。但是,当我尝试使用 sys.stdout.seek 时,无论我给它什么参数,它都会抛出一个错误:

IOError: [Errno 29] Illegal seek 

这是怎么回事?是不是我使用的是 TTY 本身而不是像 xterm 这样的虚拟终端?我该如何解决这个问题?

请您参考如下方法:

当 stdout 是 TTY 时,它是字符设备并且不可搜索,您不能在 TTY 中搜索。 tell也是一样,没有实现:

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

但是,当您将标准流重定向到一个文件时,例如在 shell 中:

$ my program > ouput.txt 

现在你可以从标准输出seek,标准输出现在是一个文件。

如果您使用的是 Unix,请查看此引用资料:

Understanding character device (or character special) files .