为什么在Python文件处理中,文件的总大小和指针的当前位置存在冲突?

为什么在Python文件处理中,文件的总大小和指针的当前位置存在冲突?,python,python-3.x,Python,Python 3.x,我的档案内容如下: 1 2 34 56 78 以下是我编写的代码: file_name = "abc.txt" file_mode = "r" with open(file_name, file_mode) as f: my_list = list(f.read()) print(my_list) print("length of the file is", len(my_list)) print("position being told by .te

我的档案内容如下:

1   2   
34
56
78
以下是我编写的代码:

file_name = "abc.txt"
file_mode = "r"
with open(file_name, file_mode) as f:
    my_list = list(f.read())
    print(my_list)
    print("length of the file is", len(my_list))
    print("position being told by .tell() method is", f.tell())
代码的输出:

['1', '\t', '2', '\t', '\n', '3', '4', '\n', '5', '6', '\n', '7', '8', '\n']
length of the file is 14
position being told by .tell() method is 18

你看到这里怎么了吗?当我的文件内容的总长度不超过14时,为什么tell方法将指针的位置返回为18?

您以文本模式打开了文件,该模式将文件中的
\r\n
转换为
\n
中由
f.read()
返回的值。共有4行,与不一致性相匹配

以二进制模式打开它,您将得到一致的结果

file_mode = "rb"