read the last line of a file in python

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

    You can read and edit all the line doing something like:

    file = open('your_file.txt', 'r')
    read_file = file.readlines()
    file.close()
    
    file1 = open('your_file.txt', 'w')
    
    var = 'filename.txt'
    
    for lec in range(len(read_file)):
        if lec == 1:
            file1.write('' % var)
        else:
            file1.write(read_file[lec])
    file1.close()
    

提交回复
热议问题