How to read a file in reverse order?

前端 未结 21 2527
礼貌的吻别
礼貌的吻别 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:44

    a simple function to create a second file reversed (linux only):

    import os
    def tac(file1, file2):
         print(os.system('tac %s > %s' % (file1,file2)))
    

    how to use

    tac('ordered.csv', 'reversed.csv')
    f = open('reversed.csv')
    

提交回复
热议问题