gpt4 book ai didi

python - file.write() 跳转到随机位置 python 2.7

转载 作者:行者123 更新时间:2023-11-28 20:23:51 24 4
gpt4 key购买 nike

我在解释器中玩耍,file.write() 方法表现得很奇怪,我希望有人能解释一下。

>>> file.seek(0)
>>> file.tell()
0
>>> file.readline()
'The Project Gutenberg EBook of The Adventures of Sherlock Holmes\n'
>>> file.tell()
65
>>> file.realine()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'file' object has no attribute 'realine'
>>> file.readline()
'by Sir Arthur Conan Doyle\n'
>>> file.tell()
91
>>> file.write("line\n")
>>> file.tell()
4101
>>>

为什么 file.write("line\n") 让它跳到 4101?

文件是这个的副本:http://norvig.com/big.txt以r+模式打开

最佳答案

一些 Python 实现使用 read-ahead buffer for readline.所以在调用 readline 之后,文件句柄的位置不一定刚读取的行的末尾。

关于python - file.write() 跳转到随机位置 python 2.7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18727182/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com