使用Python读取文件时
f = open("文件名.txt")
并通过以下方式访问数据
f.read(1)
最后找到流usibg的位置
f.tell()
每一步;我们得到从 0 开始到当前位置的连续编号。
我面临的问题是,对于某些位置,我实际上得到了一个随机数作为f.tell()
,然后继续这些数字。
例如,f.tell()
输出如下所示
0
1
2
3
133454568679978
6
7
8...
知道为什么会发生这种情况吗?
我的代码:
f=open("temp_mcompress.cpp")
current = ' '
while current != '' :
print(f.tell())
current = f.read(1)
f.close()
Temp_mcompress.cpp 文件:
#include <iostream>
int main(int a)
{
}
输出:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 号
18
18446744073709551636
18446744073709551638
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
18446744073709551655
40
41
43
44
最佳答案
看来我可能已经发现了可能仍然适用于 python 3.x 的问题: 来源:http://docs.python.org/2.4/lib/bltin-file-objects.html
tell()
Return the file's current position, like stdio's ftell().
Note: On Windows, tell() can return illegal values (after an fgets()) when reading files with Unix-style line-endings. Use binary mode ('rb') to circumvent this problem.
关于string - Python中的文件读取问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16930693/