title: Reading from a frequently updated file
body: <p>I'm currently writing a program in python on a Linux system. The objective is to read a log file and execute a bash command upon finding a particular string. The log file is being constantly written to by another program.</p>
  <p><em>My question</em>: If I open the file using the <code>open()</code> method will my Python file object be updated as the actual file gets written to by the other program or will I have to reopen the file at timed intervals?</p>
  <p><em>UPDATE</em>: Thanks for answers so far. I perhaps should have mentioned that the file is being written to by a Java EE app so I have no control over when data gets written to it. I've currently got a program that reopens the file every 10 seconds and tries to read from the byte position in the file that it last read up to. For the moment it just prints out the string that's returned. I was hoping that the file did not need to be reopened but the read command would somehow have access to the data written to the file by the Java app.</p>
  <pre><code>#!/usr/bin/python
  import time
  
  fileBytePos = 0
  while True:
      inFile = open('./server.log','r')
      inFile.seek(fileBytePos)
      data = inFile.read()
      print data
      fileBytePos = inFile.tell()
      print fileBytePos
      inFile.close()
      time.sleep(10)
  </code></pre>
  <p>Thanks for the tips on pyinotify and generators. I'm going to have a look at these for a nicer solution.</p>