python - 我无法 sys.stdout.seek

标签 python stdout tty

据我所知,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

现在您可以从 stdout 中seek,stdout 现在是一个文件。

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

Understanding character device (or character special) files

关于python - 我无法 sys.stdout.seek,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45354873/

相关文章:

python - 从 python 和 launchd 启动 osascript

stdout - 将 SystemVerilog $display 发送到 stderr

javascript - 如何从 Node.js 连接到 nethack?

python - 如何在 Python 3 中将用户输入的文本显示为不同的字符?

python - 从 Django 中的 DateTime 中删除时间

python - Django-registration and ReCaptcha integration - 如何传递用户的IP

python - 我调用函数的次数越多,函数中变量的值就不断增加

IIS下Python FastCGI - stdout写入问题

java - 在java中读取进程的实时输出

php - 从 TTY 到 PHP 的 AWK 管道输出