How to read a file in reverse order?

前端 未结 21 2586
礼貌的吻别
礼貌的吻别 2020-11-22 04:51

How to read a file in reverse order using python? I want to read a file from last line to first line.

21条回答
  •  太阳男子
    2020-11-22 05:44

    Most of the answers need to read the whole file before doing anything. This sample reads increasingly large samples from the end.

    I only saw Murat Yükselen's answer while writing this answer. It's nearly the same, which I suppose is a good thing. The sample below also deals with \r and increases its buffersize at each step. I also have some unit tests to back this code up.

    def readlines_reversed(f):
        """ Iterate over the lines in a file in reverse. The file must be
        open in 'rb' mode. Yields the lines unencoded (as bytes), including the
        newline character. Produces the same result as readlines, but reversed.
        If this is used to reverse the line in a file twice, the result is
        exactly the same.
        """
        head = b""
        f.seek(0, 2)
        t = f.tell()
        buffersize, maxbuffersize = 64, 4096
        while True:
            if t <= 0:
                break
            # Read next block
            buffersize = min(buffersize * 2, maxbuffersize)
            tprev = t
            t = max(0, t - buffersize)
            f.seek(t)
            lines = f.read(tprev - t).splitlines(True)
            # Align to line breaks
            if not lines[-1].endswith((b"\n", b"\r")):
                lines[-1] += head  # current tail is previous head
            elif head == b"\n" and lines[-1].endswith(b"\r"):
                lines[-1] += head  # Keep \r\n together
            elif head:
                lines.append(head)
            head = lines.pop(0)  # can be '\n' (ok)
            # Iterate over current block in reverse
            for line in reversed(lines):
                yield line
        if head:
            yield head
    

提交回复
热议问题