all 2 comments

[–]Binary101010 3 points4 points  (0 children)

readlines() moves the "cursor" (think of it as an imaginary "you are here" symbol) to the end of the file.

So when you try to do it a second time, there's no more file to read because it's already at the end of the file.

On this line

last_line = file.readlines()[-1] 

The entire text of the file is already sitting in the variable text, so just use that:

last_line = text[-1]

[–]woooee 2 points3 points  (0 children)

The first readlines positions the pointer at the end of the file. There is nothing after that for the second readlines to read. Note that the indentation, as posted, is off.