首页 > 编程语言 > 详细

python 逆序按行读取文件

时间:2020-12-18 16:54:43      阅读:205      评论:0      收藏:0      [点我收藏+]

How to read a file in reverse order?

import os

def readlines_reverse(filename):
    with open(filename) as qfile:
        qfile.seek(0, os.SEEK_END)
        position = qfile.tell()
        line = ‘‘
        while position >= 0:
            qfile.seek(position)
            next_char = qfile.read(1)
            if next_char == "\n":
                yield line[::-1]
                line = ‘‘
            else:
                line += next_char
            position -= 1
        yield line[::-1]


if __name__ == ‘__main__‘:
    for qline in readlines_reverse(raw_input()):
        print qline

python 逆序按行读取文件

原文:https://www.cnblogs.com/hangj/p/14155033.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!