"일꾼이 일을 잘하려면 먼저 도구를 갈고 닦아야 한다." - 공자, 『논어』.
첫 장 > 프로그램 작성 > How Can I Efficiently Read a Large File in Reverse Order Using Python?

How Can I Efficiently Read a Large File in Reverse Order Using Python?

2025-03-23에 게시되었습니다
검색:395

How Can I Efficiently Read a Large File in Reverse Order Using Python?

Reading a File in Reverse Order in Python

If you're working with a large file and need to read its contents from the last line to the first, Python's built-in functions may not be suitable. Here's an efficient solution to tackle this task:

Reverse Line Reader Generator

The following code defines a generator function, reverse_readline, which yields the lines of a file in reverse order. It uses a buffer-based approach to optimize performance and handle large files effectively:

import os

def reverse_readline(filename, buf_size=8192):
    """A generator that returns the lines of a file in reverse order"""
    with open(filename, 'rb') as fh:
        segment = None
        offset = 0
        fh.seek(0, os.SEEK_END)
        file_size = remaining_size = fh.tell()
        while remaining_size > 0:
            offset = min(file_size, offset + buf_size)
            fh.seek(file_size - offset)
            buffer = fh.read(min(remaining_size, buf_size))
            # remove file's last "\n" if it exists, only for the first buffer
            if remaining_size == file_size and buffer[-1] == ord('\n'):
                buffer = buffer[:-1]
            remaining_size -= buf_size
            lines = buffer.split('\n'.encode())
            # append last chunk's segment to this chunk's last line
            if segment is not None:
                lines[-1] += segment
            segment = lines[0]
            lines = lines[1:]
            # yield lines in this chunk except the segment
            for line in reversed(lines):
                # only decode on a parsed line, to avoid utf-8 decode error
                yield line.decode()
        # Don't yield None if the file was empty
        if segment is not None:
            yield segment.decode()

Usage:

To use this generator, you can simply iterate over it in a for loop:

for line in reverse_readline('myfile.txt'):
    print(line)

This will print the lines of the file in reverse order.

최신 튜토리얼 더>

부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.

Copyright© 2022 湘ICP备2022001581号-3