How to read a file in reverse order?

前端 未结 21 3002
礼貌的吻别
礼貌的吻别 2020-11-22 04:51

How to read a file in reverse order using python? I want to read a file from last line to first line.

21条回答
  •  温柔的废话
    2020-11-22 05:38

    for line in reversed(open("file").readlines()):
        print line.rstrip()
    

    If you are on linux, you can use tac command.

    $ tac file
    

    2 recipes you can find in ActiveState here and here

提交回复
热议问题