0

I am trying to make a plugin for a video-game, and all inputs the program takes are taken from a log-file provided by the server. (the plugin is running in windows)

Sometimes, the server appends the log-file too quickly and my script isn't able to keep up. I do have a separate thread continiously reading the file. I've tried several solutions like:

def get_line():
last_line = ""
with open(mypath1 + "log.log", "r") as f:
    for line in f:
        pass
    last_line = line[:-1]
    return last_line

But it isn't quite quick enough. I am in no way a professional programmer, just learning as i go. Any suggestions would be much appreciated

blake_won
  • 29
  • 3
  • This post might be helpful to you, [Reading changing file in Python 3 and Python 2](https://stackoverflow.com/questions/40364643/reading-changing-file-in-python-3-and-python-2) – DumTux Feb 15 '20 at 13:15
  • Thank you, but my issue is speed, not functionality – blake_won Feb 15 '20 at 13:27
  • Welcome to SO @blake_won! Please edit your question to clarify what you want to accomplish. For example, "How can guarantee that I always have the latest information from the log file?" – Jesuisme Feb 15 '20 at 15:04

1 Answers1

0

Try keeping the file open and only continue reading instead of always rereading from the start of the file:

def get_line(f=open('log.log')):
    for line in f:
        pass
    return line[:-1]

Demo (also runnable at repl.it):

def write(*lines):
    with open('log.log', 'a') as f:
        for line in lines:
            print(line, file=f)

write('foo', 'bar', 'last1')
print(get_line())
write('foo', 'bar', 'last2')
print(get_line())

Output:

last1
last2

More robust version that returns an empty string if the file is empty, and repeats the previous last line if that's still the last line (i.e., if there are no new lines):

def get_line(f=open('log.log'), cache=['']):
    for cache[0] in f:
        pass
    return cache[0][:-1]

Demo at repli.it.

Kelly Bundy
  • 23,480
  • 7
  • 29
  • 65
  • i'll give that a shot, i'll tell you my findings in a jiffy! – blake_won Feb 15 '20 at 14:02
  • @blake_won Try the new version at the end as well, that's probably better. The first version is rather a proof of concept. – Kelly Bundy Feb 15 '20 at 14:35
  • i tried replicating the issue that i've been having, your code seems to work like a charm, thanks a lot ! one question though if you would indulge me, my "with open as f" code is opening a file every time, i get that, but why doesn't yours? it has f=open() in the argument – blake_won Feb 15 '20 at 14:46
  • @blake_won Default argument are evaluated at the time the function is defined, not every time the function is called. It can be a [gotcha](https://docs.python-guide.org/writing/gotchas/) but you can also use it to your advantage. – Kelly Bundy Feb 15 '20 at 14:51
  • Thank you big time, i am 600 lines into the script and this is a real life saver, not only did you basically fix it for me, but you taught me something new as well. you're a prime example of what stackoverflow should be – blake_won Feb 15 '20 at 14:56