Get last n lines of a file, similar to tail

前端 未结 30 2538
挽巷
挽巷 2020-11-22 03:46

I\'m writing a log file viewer for a web application and for that I want to paginate through the lines of the log file. The items in the file are line based with the newest

30条回答
  •  别那么骄傲
    2020-11-22 04:00

    S.Lott's answer above almost works for me but ends up giving me partial lines. It turns out that it corrupts data on block boundaries because data holds the read blocks in reversed order. When ''.join(data) is called, the blocks are in the wrong order. This fixes that.

    def tail(f, window=20):
        """
        Returns the last `window` lines of file `f` as a list.
        f - a byte file-like object
        """
        if window == 0:
            return []
        BUFSIZ = 1024
        f.seek(0, 2)
        bytes = f.tell()
        size = window + 1
        block = -1
        data = []
        while size > 0 and bytes > 0:
            if bytes - BUFSIZ > 0:
                # Seek back one whole BUFSIZ
                f.seek(block * BUFSIZ, 2)
                # read BUFFER
                data.insert(0, f.read(BUFSIZ))
            else:
                # file too small, start from begining
                f.seek(0,0)
                # only read what was not read
                data.insert(0, f.read(bytes))
            linesFound = data[0].count('\n')
            size -= linesFound
            bytes -= BUFSIZ
            block -= 1
        return ''.join(data).splitlines()[-window:]
    

提交回复
热议问题