Python读取大文件的最后N行

来源:互联网 发布:windows 7破解 编辑:程序博客网 时间:2024/05/21 19:39


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('\n')
    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

0 0
原创粉丝点击