首页 » Linux » Python读取大文件最后几行

Python读取大文件最后几行

 
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#desc: read a big file last line

import os

def file_get_last_lines(file_path, num):
    num = int(num)
    blk_size_max = 4096 #分区的block大小
    n_lines = []
    with open(file_path, 'rb') as fp:
        fp.seek(0, os.SEEK_END)
        cur_pos = fp.tell()
        while cur_pos > 0 and len(n_lines) < num:
            blk_size = min(blk_size_max, cur_pos)
            fp.seek(cur_pos - blk_size, os.SEEK_SET)
            blk_data = fp.read(blk_size)
            assert len(blk_data) == blk_size
            lines = blk_data.split(b'\n')

            # adjust cur_pos
            if len(lines) > 1 and len(lines[0]) > 0:
                n_lines[0:0] = lines[1:]
                cur_pos -= (blk_size - len(lines[0]))
            else:
                n_lines[0:0] = lines
                cur_pos -= blk_size

            fp.seek(cur_pos, os.SEEK_SET)

    if len(n_lines) > 0 and len(n_lines[-1]) == 0:
        del n_lines[-1]
    return n_lines[-num:]

if __name__ == '__main__':
    filepath='/data/logs/nginx.log'
    num = 10
    lines = file_get_last_lines(filepath,num)
    for line in lines:
        print(line.decode('utf-8')) #读出来的是二进制,需要解码一下变成字符串

原文链接:Python读取大文件最后几行,转载请注明来源!

0