Reading a file that is changing and getting the new lines

Ouray Viney oviney at gmail.com
Tue Dec 1 18:09:26 EST 2009


Hi:

Problem:
=========
I want to read a ASCII text file that can have data appended to it.  I
have hacked some code together that handles the basics, but it falls
short.  My code doesn't read in the new lines that could have been
added to the end of the file.  Not python's fault, more that I don't
know how to do it in python.

Example scenario:  As the python script is running a user/application
adds new entries to the end of the test case file, example, adds the
following to the file.

test4
test5
test6

The python code below, won't pick these changes up.

Analysis:
========
I understand why my code doesn't handle the addition of new data.
This is simply because the os.open reads all the lines in the
beginning and since the data is added after that, the for loop doesn't
iterate through it since it doesn't know about it.  Please note, my
code is not pretty and isn't at all advanced.

Code:
======
import os,string,sys,time

# define locals
tcListFile="testcase_list.txt"
fh=open(tcListFile,"r")
tcList=fh.readlines()
fh.close()

for tc in tcList:
	print tc.strip()
	print "sleeping for 2 seconds"
	time.sleep(2)

# close the file handle
fh.close()

text file:
======
test1
test2
test3

Question:
=========
What is the best way to handle this type of scenario using python?



More information about the Python-list mailing list