Python實現(xiàn)linux的tail功能

基于python3,簡單實現(xiàn)tail -n、tail -f功能

原理

  1. 把文件大小分頁進行讀取,這樣讀取大日志就無需全部加載到內(nèi)存中
  2. 文件倒讀輸出

代碼實現(xiàn)

#!/usr/bin/env python
# coding: utf-8
"""
原理:
    https://yq.aliyun.com/articles/60684
功能:
    實現(xiàn)tail -n
    實現(xiàn)tail -f
其它實現(xiàn):
    http://www.cnblogs.com/bufferfly/p/4878688.html
    https://github.com/shengxinjing/my_blog/issues/11
BUG:
    重定向相同數(shù)據(jù)到日志文件里,使用>而不是>>的話,輸入無法打印出來
"""

import os
import sys
import time

PAGE = 4096

class Tail:
    def __init__(self, filename, callback=sys.stdout.write):
        self.filename = filename
        self.callback = callback

    def reverse(self, n=10):
        """
        實現(xiàn) tail -n
        """
        with open(self.filename, 'rb') as f:
            f_len = f.seek(0, 2)
            rem = f_len % PAGE
            page_n = f_len // PAGE
            r_len = rem if rem else PAGE
            while True:
                # 如果讀取的頁大小>=文件大小,直接讀取數(shù)據(jù)輸出
                if r_len >= f_len:
                    f.seek(0)
                    lines = f.readlines()[::-1]
                    break

                f.seek(-r_len, 2)
                # print('f_len: {}, rem: {}, page_n: {}, r_len: {}'.format(f_len, rem, page_n, r_len))
                lines = f.readlines()[::-1]
                count = len(lines) -1   # 末行可能不完整,減一行,加大讀取量

                if count >= n:  # 如果讀取到的行數(shù)>=指定行數(shù),則退出循環(huán)讀取數(shù)據(jù)
                    break
                else:   # 如果讀取行數(shù)不夠,載入更多的頁大小讀取數(shù)據(jù)
                    r_len += PAGE
                    page_n -= 1

        for line in lines[:n]:
            self.callback(line.decode('utf-8'))

    def follow(self):
        """
        實現(xiàn) tail -f
        """
        with open(self.filename, 'rb') as fd:
            pos = fd.seek(0, 2)  # 打開文件時大小
            try:
                while True:
                    curr_pos = fd.seek(0,2)
                    # print('pos: {}, curr_pos: {}'.format(pos, curr_pos))
                    if pos > curr_pos:  # 表示文件數(shù)據(jù)減少或清空
                        pos = fd.seek(0, 2)
                        # time.sleep(0.3)
                        continue

                    line = fd.readline()
                    if line:
                        self.callback(line.decode('utf-8'))
                    # time.sleep(0.1)
            except KeyboardInterrupt as e:
                pass

if __name__ == '__main__':
    if len(sys.argv) != 3:
        print('Usage: {} [ -f | -# ] file'.format(sys.argv[0]), file=sys.stderr)
        raise SystemExit(1)

    if not os.path.isfile(sys.argv[2]):
        print('File does not exist.')
        raise SystemExit(1)
    else:
        tail = Tail(sys.argv[2])

    if '-f' == sys.argv[1]:
        tail.reverse()
        tail.follow()
    elif '-' in sys.argv[1]:
        try:
            n = int(sys.argv[1].strip('-'))
            tail.reverse(n)
        except ValueError:
            print('Unknown command {!r}'.format(sys.argv[1]), file=sys.stderr)
            raise SystemExit(1)
    else:
        print('Unknown command {!r}'.format(sys.argv[1]), file=sys.stderr)
        raise SystemExit(1)

測試

  1. 生成測試數(shù)據(jù)
with open('test.log', 'w+') as f:
    for l in range(1, 21):
        print('This is line {}'.format(l), file=f )
  1. 讀取測試
[tail] python test_data.py                                            15:49:04
[tail] python tail.py -5 test.log                                     15:50:09
This is line 20
This is line 19
This is line 18
This is line 17
This is line 16
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.創(chuàng)建文件夾 !/bin/sh mkdir -m 777 "%%1" 2.創(chuàng)建文件 !/bin/sh touch...
    BigJeffWang閱讀 10,495評論 3 53
  • http://192.168.136.131/sqlmap/mysql/get_int.php?id=1 當(dāng)給sq...
    xuningbo閱讀 10,562評論 2 22
  • sqlmap用戶手冊 說明:本文為轉(zhuǎn)載,對原文中一些明顯的拼寫錯誤進行修正,并標(biāo)注對自己有用的信息。 ======...
    wind_飄閱讀 2,207評論 0 5
  • 最近因為寫字,遇到許多喜歡的人,各行各業(yè)。淘寶店主,企業(yè)白領(lǐng),餐飲老板,教師,公務(wù)員等等,不同的人生軌跡,因為有相...
    雪靈溪閱讀 873評論 19 15
  • 當(dāng)心情不好的時候 不要吵架 不要給他人帶去不悅 不要著急發(fā)表意見 因為腦子會短路 當(dāng)心情不好的時候 去看看書吧 沉...
    白豐閣閱讀 201評論 0 4

友情鏈接更多精彩內(nèi)容