0

I am opening a text file, which once created is constantly being written to, and then printing this out to a console any new lines, as I don't want to reprint the whole text file each time. I am checking to see if the file grows in size, if it is, just print the next new line. This is mostly working, but occasionally it gets a bit confused about the next new line, and new lines appear a few lines up, mixed in with the old lines. Is there a better way to do this, below is my current code.

infile = "Null"
while not os.path.exists(self.logPath):
    time.sleep(.1)

if os.path.isfile(self.logPath):
    infile = codecs.open(self.logPath, encoding='utf8')
else:
    raise ValueError("%s isn't a file!" % file_path)

lastSize = 0
lastLineIndex = 0
while True:
    wx.Yield()
    fileSize = os.path.getsize(self.logPath)
    if fileSize > lastSize:
        lines = infile.readlines()
        newLines = 0
        for line in lines[lastLineIndex:]:
            newLines += 1
            self.running_log.WriteText(line)

        lastLineIndex += newLines

        if "DBG-X: Returning 1" in line:
            self.subject = "FAILED! - "
            self.sendEmail(self)
            break
        if "DBG-X: Returning 0" in line:
            self.subject = "PASSED! - "
            self.sendEmail(self)
            break
    fileSize1 = fileSize
    infile.flush()
    infile.seek(0)

infile.close()

Also my application freezes whilst waiting for the text file to be created, as it takes a couple of seconds to appear, which isn't great.

Cheers.

speedyrazor
  • 3,127
  • 7
  • 33
  • 51

2 Answers2

0

Maybe you could:

  • open the file each time you need to read in it,
  • use lastSize as argument to seek directly to where you stopped at last reading.

Additional comment: I don't know if you need some protection, but I think you should not bother to test whether given filename is a file or not; just open it in a try...except block and catch problems if any.

As for the freezing of your application, you may want to use some kind of Threading, for instance: one thread, your main one, is handling the GUI, and a second one would wait for the file to be created. Once the file is created, the second thread sends signals to the GUI thread, containing the data to be displayed.

Joël
  • 2,723
  • 18
  • 36
0

This solution could help. You'd also have to do a bit of waiting until the file appears, using os.path.isfile and time.sleep.

Community
  • 1
  • 1
Unknown
  • 5,722
  • 5
  • 43
  • 64