python 逆序按行读取文件

python

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 逆序按行读取文件 的全部内容, 来源链接: utcz.com/z/387786.html

回到顶部