Python读取大文件的最后N行
import linecache
filename = "/root/log_history.txt"
# 放入缓存防止内存过载
def get_line_count(filename):
line_count = 0
file = open(filename,"r+")
while True:
buffer = file.read(8192 * 1024)
if not buffer:
break
line_count += buffer.count("
")
file.close()
return line_count
if __name__ == "__main__":
n = 30 #get the last 30 lines
linecache.clearcache()
line_count = get_line_count(filename)
print("line count total:",line_count)
line_count = line_count - 30
print("line_count:[%s]" % line_count)
for i in range(n+1): #the last 30 lines
last_line = linecache.getline(filename, line_count)
print("line:[%s],%s" % (line_count,last_line))
line_count += 1