在下面的代码中,f.write(...)
返回60
,但紧随其后的f.tell()
返回63
。
我无法理解为什么3
有这种差异。有人可以解释一下。在Windows上使用Python 3.5。
>>> f = open('c:\\tmp\\tut.txt', 'w')
>>>
>>> f.write(''' My name is Bot
... I am writing a file
... To read it back
... ''')
60
>>> f.tell()
63
答案 0 :(得分:0)
请尝试将b
添加到您的打开文件语句中:
>>> f = open('c:\\tmp\\tut.txt', 'wb') # change 'w' -> 'wb'
并确保您的问题消失了。因为默认情况下,您是在文本模式下打开文件的,因此LF会扩展为CRLF。