如何以相反的顺序读取文件? [英] How to read a file in reverse order?
本文介绍了如何以相反的顺序读取文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用python反向读取文件?我想从最后一行读取文件.
How to read a file in reverse order using python? I want to read a file from last line to first line.
推荐答案
for line in reversed(open("filename").readlines()):
print line.rstrip()
在Python 3中:
And in Python 3:
for line in reversed(list(open("filename"))):
print(line.rstrip())
这篇关于如何以相反的顺序读取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文