Reading streaming data sources
What if the data that is coming from the source is continuous? What if we need to read continuous data? This recipe will demonstrate a simple solution that will work for many common real-life scenarios, though it is not universal and you will need to modify it if you hit a special case in your application.
How to do it...
In this recipe, we will show you how to read an always-changing file and print the output. We will use the common Python module to accomplish this.
import time import os import sys if len(sys.argv) != 2: print >> sys.stderr, "Please specify filename to read" filename = sys.argv[1] if not os.path.isfile(filename): print >> sys.stderr, "Given file: \"%s\" is not a file" % filename with open(filename,'r') as f: # Move to the end of file filesize = os.stat(filename)[6] f.seek(filesize) # endlessly loop while True: where = f.tell() # try reading a line line = f.readline() ...