0

I am making a python program for a Minecraft server that automatically bids on items up to a certain price. In appdata/roaming/.minecraft/logs there is a chat log called "latest.log". This log is constantly being updated with what everyone on my server is saying over chat. If I open it and view the text, the log doesn't automatically update (obviously). How would I use a python script to print every line in my log and automatically update?

I am on Windows 8.1 with Python 2.7.9

  • See the answer to this [question](http://stackoverflow.com/questions/5419888/reading-from-a-frequently-updated-file). – syntonym Feb 18 '15 at 18:36

2 Answers2

0

The data isn't written to the latest.log file until the process writing it (probably the server) fills or flushes the buffer. There probably isn't any way to change that from within Python. The best bet is to see if you can configure the writing process to flush after each line.

Tom Hunt
  • 916
  • 7
  • 21
  • I'm not sure entirely that I am following you but I have open and closed the log very fast with a python script and it scrolls all the way down to the bottom to see the newest update in the chat and the log appears to update almost immediately. – Nathaniel Sharp Feb 18 '15 at 23:05
0

So, I tried a few things and figured out I could just do this to follow the logfile (update it constantly)

def follow(thefile):
    thefile.seek(0,2)
    while True:
        line = thefile.readline()
        if not line:
            time.sleep(0.1)
            continue
        yield line

def beginread():
    loglines = follow(logfile)
    for line in loglines:
        print line

And in IDLE I would simple start the function named beginread. With this program running, it will print lines from the log file constantly as it is updated.