read the last line of a file in python

后端 未结 6 1880
[愿得一人]
[愿得一人] 2021-02-01 09:25

I have a two requirements .

First Requirement-I want to read the last line of a file and assign the last value to a variable in python.

6条回答
  •  感情败类
    2021-02-01 10:05

    Why you just read all the lines and store the last line to the variable?

    with open('filename.txt', 'r') as f:
        last_line = f_read.readlines()[-1]
    

提交回复
热议问题