0

How do i just read the last line, while a file is getting updated? I saw solution which waited for one second to read again. But i want to run a function each time a line is added if the line contains a chasephrase? How do i do that? I tried the following

f1 = open('file.txt', 'r')
while True:
    for line in f1:
        pass
    print(line)

which prints me the last line constantly. How do i implement this functionality while it only do something (print in the moment) once a new line is added and only once?

Tollpatsch
  • 304
  • 4
  • 13
  • `lastLine = f1[-1]` –  Sep 27 '20 at 16:17
  • i get the error message _io.TextIOWrapper' object is not subscriptable – Tollpatsch Sep 27 '20 at 16:22
  • Oh sorry by f1 i meant file after you read it. –  Sep 27 '20 at 16:24
  • If you just iterate on the file, Python will read the file until EOF as normally that is what you'd want. Instead of doing that you can read the file manually, using f.read() and keep up with the updates. I'll put this in a pastebin since the question got closed: https://pastebin.com/Xvvcr7BW – gergelykalman Sep 27 '20 at 16:24

0 Answers0