python - "OSError: telling position disabled by next() call"错误的含义?

标签 python python-3.x file

这几乎是同一个问题 How to solve "OSError: telling position disabled by next() call" .虽然较旧的问题已经收到了一些有用的解决方法的答案,但错误的含义尚不清楚。我想知道是否有人可以对此发表评论。

我正在学习 Python 并松散地关注 tutorial .我在 Fedora 23 上以交互方式输入以下内容:

$ python3
Python 3.4.3 (default, Aug  9 2016, 15:36:17)
[GCC 5.3.1 20160406 (Red Hat 5.3.1-6)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> with open("myfile","r") as file:
...     for l in file:
...         print("Next line: \"{}\"".format(l))
...         print("Current position {:4d}".format(file.tell()))

myfile 包含几行文本。输出:

Next line: "This is line number 0
"
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
OSError: telling position disabled by next() call

谷歌搜索此错误会产生多达 6 个结果。 Windows 10 上的 Cygwin 上的 Python 3.6.4 也是如此。

编辑:

文本文件的tell()方法是documented as follows :

Return the current stream position as an opaque number. The number does not usually represent a number of bytes in the underlying binary storage.

“不透明数字”似乎表明我不能只打印它。因此,我用 pos = file.tell() 替换了第二个 print() 调用。相同的结果。

最佳答案

该消息的意思与它所说的完全一致:因为您已经在该文件上调用了 next(),所以该文件上的 tell() 已被禁用。

它可能看起来不像您调用了 next,但 for 循环隐式调用了它。 for 循环:

for element in thing:
    do_stuff_with(element)

是语法糖

iterator = iter(thing) # the real implementation doesn't use a variable
while True:
    try:
        element = next(iterator) # here's the next() call
    except StopIteration:
        break
    do_stuff_with(element)

对于文件,iter(file) 返回文件,循环调用文件上的next


至于为什么调用next会禁用tell(),这是为了效率。它只发生在文本文件(特别是 io.TextIOWrapper),它必须执行 bunch。的 extra努力支持 tell;关闭 tell 支持可以让他们跳过这项工作。 change 的原始提交消息让 next 禁用 tell 的是“Speed up next() by disabling snapshot updating then.”,表明这是为了提高效率。

对于历史背景,previous Python versionsnext 使用了一个隐藏的缓冲区,而 tell 和其他文件方法没有考虑到,导致 tell (和其他文件方法)产生 not - 对文件进行迭代期间非常有意义的结果。当前的 IO 实现将能够在迭代期间支持 tell(),但是 io.TextIOWrapper 无论如何都会阻止此类调用。 next 和其他方法之间的历史不兼容性可能导致了为什么在迭代期间禁用部分文件功能被认为是合理的。


您没有要求解决方法,但为了最终在此页面上寻找解决方法的人的利益,我会提到这一点

for line in iter(file.readline, ''):
    ...

将允许您在不禁用 tell 的情况下遍历文本文件的行。 (对于二进制文件,你可以使用 for line in iter(file.readline, b''),但是没有多大意义,因为 tell 禁用机制不存在用于二进制文件。)

关于python - "OSError: telling position disabled by next() call"错误的含义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49785865/

相关文章:

c++ - 在 C++ 中的单独文件中拥有命名空间的正确方法是什么?

python - Python 异常后是否需要返回语句?

python - 为什么 pip 在已经是最新版本时要求我升级?

Python vs Bash - 在哪种任务中,每个任务的性能都超过了另一个?

python - 逐周、逐日和逐年获取

python - 将图形转换为字典形式

python - python函数的分析

c - 有没有办法规范化不存在文件的文件路径?

python - Django教程错误: Setting up the test client

mysql - 在 Mac 命令行上,如何执行文件 foo.sql 中的 SQL 语句并将结果以制表符分隔形式导出到另一个文件中?