pythonfilereverse

How to read a file in reverse order?


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


Solution

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

    And in Python 3:

    for line in reversed(list(open("filename"))):
        print(line.rstrip())