Search code examples
pythonfilereadlines

Wait for text file to be written, read changes line by line and print


I have the below code, which waits for a log file to appear, it's being saved there by an external programme, when the log file appears I open it, while it's still being written to, and print the contents, as the log file is being updated by the external programme I want to print out any new lines being written to it. Currently I clear the print out and re-print the entire text, which I would like to avoid, I would like to only print the new lines. Another thing to improve is waiting for the file to appear, rather than just pausing the python script.

    a=0
    while a <= 10:
        if os.path.isfile(logPath):
            infile = file(logPath)
            break
        a=a+1
        time.sleep(1)
    fileSize1 = 0
    while True:
        wx.Yield()
        fileSize = os.path.getsize(logPath)
        if fileSize > fileSize1:
            lines = infile.readlines()
            log.Clear()
            log.Refresh()
            for line in lines:
                print line.rstrip()
            if "DBG-X: Returning 1" in line:
                break
            if "DBG-X: Returning 0" in line:
                break
        fileSize1 = fileSize
        infile.seek(0)

Solution

  • Try something like this...

    lastSize = 0
    lastLineIndex = 0
    while True:
        wx.Yield()
        fileSize = os.path.getsize(logPath)
        if fileSize > lastSize:
            lines = infile.readlines()
            newLines = 0
            for line in lines[lastLineIndex:]:
                newLines += 1
                print line.rstrip()
            lastLineIndex += newLines
    
            if "DBG-X: Returning 1" in line:
                break
            if "DBG-X: Returning 0" in line:
                break
        fileSize1 = fileSize
        infile.seek(0)
    

    The key bit is

    for line in lines[lastLineIndex:]:
    

    Where we skip the lines we've already seen. You can probably skip the newLines variable and just do lastLineIndex += 1 but I've got an aversion to changing lastLineIndex inside the for loop (a habit I've picked up to avoid issues in other languages)