def reversed_lines ( path ) : with open ( path , 'r' ) as handle : part = '' for block in reversed_blocks ( handle ) : for c in reversed ( block ) : if c == '\n' and part : yield part [ : : - 1 ] part = '' part += c if part : yield part [ : : - 1 ]
reverse lines of a text file python
Generate the lines of file in reverse order .
def reversed_lines(path):
    """Generate the lines of file in reverse order."""
    with open(path, 'r') as handle:
        part = ''
        for block in reversed_blocks(handle):
            for c in reversed(block):
                if c == '\n' and part:
                    yield part[::-1]
                    part = ''
                part += c
        if part: yield part[::-1]