0

I have a text file that will be populated depending on a radio button selected by a user in php

I am reading from the same text file, whenever I find that a new line has been added the Python script prints it. For now I am just printing it. Eventually I will send out a message on a GSM modem based on the new records in the .txt file. Right now I'm just printing it to make sure I can identify a new record.

Here's the code I'm using. It works just fine.

def main():
    flag = 0
    count = 0
    while flag == 0:
        f = open('test.txt', 'r')
        with open('test.txt') as f:
            for i, l in enumerate(f):
                pass
        nol = i + 1
        if count < nol:
            while count <= nol:
                f = open('test.txt', 'r')
                a = f.readlines(count)
                if count == 0:
                    print a[0]
                    count = count+2
                else:
                    print a[count-1]
                    count = count+1
if __name__ ==  '__main__':
    main()

I was wondering if there is a better way to do this.

Also, php will keep writing to the file. And python will keep reading from it. Will this cause a clash? Since multiple instance are open?

pradyunsg
  • 18,287
  • 11
  • 43
  • 96
Anon
  • 845
  • 5
  • 30
  • 50
  • 2
    Maybe this would be better suited for the codereview stackexchange? – UloPe May 04 '13 at 18:43
  • possible duplicate of [tail -f in python with no time.sleep](http://stackoverflow.com/questions/1475950/tail-f-in-python-with-no-time-sleep) – Aya May 04 '13 at 19:54
  • This question appears to be off-topic; you might find answers at http://codereview.stackexchange.com – Zero Piraeus Jul 20 '13 at 16:26

1 Answers1

1

According to this answer you can use watchdog package to watch over changes in the file. Alternatively you can use custom made solution using fcntl in Unix.

First solution has advantage of being cross-platform.

Community
  • 1
  • 1
Denis Malinovsky
  • 5,840
  • 1
  • 22
  • 17