读取大文件条数
def iter_count(file_name):
from itertools import (takewhile, repeat)
buffer = 1024 * 1024
with open(file_name,encoding='utf-8',errors='ignore') as f:
buf_gen = takewhile(lambda x: x, (f.read(buffer) for _ in repeat(None)))
# print(buf_gen)
x= sum(buf.count('\n') for buf in buf_gen)
print(x)
读取大文件最后一行
def last_line(file_path):
with open(file_path, 'rb') as f:
f.seek(-2, 2)
while f.read(1) != b'\n':
f.seek(-2, 1)
last_line = f.readline().decode()
print(last_line)
return last_line