read the last line of a file in python

后端 未结 6 1734
[愿得一人]
[愿得一人] 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:00

    Example from https://docs.python.org/3/library/collections.html

    from collections import deque
    
    def tail(filename, n=10):
        'Return the last n lines of a file'
        with open(filename) as f:
            return deque(f, n) 
    

提交回复
热议问题