string - Python中的文件读取问题

标签 string file-io python-3.x

使用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/

相关文章:

string - 如何在 Vala 中从 uint8[] 创建字符串?

java - 从千兆字节文件中读取

javascript - 如何在没有 I 的情况下使用正则表达式验证 A-K 范围?

python - 字符串无法正确比较

c - 为什么不推荐使用 fgets 函数?

python - 是否可以在不知道键的情况下排除 deepdiff 中的字典路径

python - 在 Docker 中运行的 Aiohttp 和 NGINX

python - 如何在 .kv 文件中使用 if 语句

java - 使用多个定界符Java分割字符串

python - 在 Python 中写入具有特定权限的文件