"OSError:告诉位置被next()调用禁用的位置"的含义是:错误? [英] Meaning of "OSError: telling position disabled by next() call" error?

查看:69
本文介绍了"OSError:告诉位置被next()调用禁用的位置"的含义是:错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这几乎与一样.解决"OSError:告诉位置被next()调用禁用" .尽管较早的问题已收到一些有用的解决方法的答案,但错误的含义尚不清楚.我想知道是否有人可以对此发表评论.

This is almost the same question as How to solve "OSError: telling position disabled by next() call". While the older question has received a few answers with useful workarounds, the meaning of the error is not clear. I wonder if anybody can comment on this.

我正在学习Python,并大致遵循教程.我在Fedora 23上以交互方式输入了以下内容:

I am learning Python and loosely following a tutorial. I entered the following interactively on 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 包含几行文本.输出:

myfile contains a few lines of text. The output:

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也会发生同样的情况.

Googling for this error yields a whopping 6 results. The same happens with Python 3.6.4 on Cygwin on Windows 10.

文本文件的 tell()方法为

以不透明数字返回当前流的位置.数字通常不表示基础二进制文件中的字节数存储.

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

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

"Opaque number" seems to indicate that I can't just print it. So, I replaced the second print() call with pos = file.tell(). Same result.

推荐答案

该消息的含义与所声明的完全相同:因为您已在文件上调用了 next(),所以使用了 tell()在该文件上已被禁用.

The message means exactly what it says: because you have called next() on the file, the use of tell() on that file has been disabled.

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

It might not look like you've called next, but the for loop calls it implicitly. A for loop:

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(文件)返回文件,并且循环在文件上调用 next .

For a file, iter(file) returns the file, and the loop calls next on the file.

至于为什么调用 next 禁用 tell(),这是为了提高效率.只有文本文件(特别是 io.TextIOWrapper )会发生这种情况,而文本文件必须执行更改使 next 禁用的原始提交消息 tell 是通过随后禁用快照更新来加快next()的速度.",表明这样做是为了提高效率.

As for why calling next disables tell(), this is for efficiency. It only happens for text files (specifically io.TextIOWrapper), which have to do a bunch of extra work to support tell; turning off tell support lets them skip that work. The original commit message for the change that made next disable tell is "Speed up next() by disabling snapshot updating then.", indicating it's for efficiency.

出于历史背景,以前的Python版本使用了 tell 和其他文件方法未考虑的 next 的隐藏缓冲区,导致 tell (和其他文件方法)产生的结果非常不正确-在文件迭代期间有意义的结果.当前的IO实现将能够在迭代期间支持 tell(),但是 io.TextIOWrapper 仍然可以阻止此类调用. next 与其他方法之间的历史不兼容性可能导致了为什么在迭代过程中禁用部分文件功能是合理的.

For historical context, previous Python versions used a hidden buffer for next that tell and other file methods didn't account for, causing tell (and other file methods) to produce not-very-meaningful results during iteration over a file. The current IO implementation would be able to support tell() during iteration, but io.TextIOWrapper prevents such calls anyway. The historical incompatibility between next and other methods likely contributed to why it was considered reasonable to disable parts of file functionality during iteration.

您没有要求解决方法,但是为了使最终在此页面上找到解决方法的人受益,我会提及

You didn't ask for workarounds, but for the benefit of people who end up on this page looking for a workaround, I'll mention that

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

将使您可以在不禁用 tell 的情况下遍历文本文件的各行.(您可以在iter(file.readline,b'')中的行中使用表示二进制文件,但是没有什么意义,因为没有 tell 禁用机制用于二进制文件.)

will let you iterate over the lines of a text file without disabling tell. (You can use for line in iter(file.readline, b'') for binary files, but there's not much point, because the tell disabling mechanism isn't there for binary files.)

这篇关于"OSError:告诉位置被next()调用禁用的位置"的含义是:错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆