源码如下:
# !/usr/bin/env python
# coding: utf-8
def read_lines(file_path, read_step=100*1024*1024):
with open(file_path, 'rb') as r:
left_s = bytes()
r.seek(0, 2)
position = r.tell()
while True:
if position < read_step:
read_step = position
position = 0
else:
position = position - read_step
r.seek(position)
c = r.read(read_step)
s = c + left_s
left_s = bytes()
for i in range(10):
try:
ss = s.decode()
break
except UnicodeDecodeError as e:
left_s += s[0:1]
s = s[1:]
else:
raise RuntimeError("Unicode error")
lines = ss.split("\n")
if len(lines) > 1:
for line in reversed(lines[1:]):
yield line
if position == 0:
yield lines[0]
break
left_s += lines[0].encode()
def main():
for line in read_lines("D:/Program/Desktop/文本/一个文本文件.txt"):
print(line)
if __name__ == '__main__':
main()
主要函数为read_lines
参数一 file_path: 表示纯文本文件路径
参数二 read_step:表示每次读取内容的字节数,值越大越占内存