Sorry for the confusion last time.
The program is supposed to read from a text file that is being written from another program. In my case the Vizard program is supposed to read a text file that Viewpoint is writing to. So I need a code that can help me to read continuously, because the text file is continuously being written, and I tried different methods with no avail.
This is my code so far:
file1 = 'file_name.txt'
file2 = open('marker.txt','a')
def linecount(fname):
""" Count number of lines in a file."""
f = open(fname)
nr_of_lines = sum(1 for line in f)
f.close()
return nr_of_lines
def readline ():
j = linecount(file1)
i = 1
count = 0
while count < 30:
j = linecount(file1)
if i <= j:
while i <= j:
line = linecache.getline(file1,i)
file2.write(line)
i = i + 1
#i = j
file2.flush()
yield viztask.waitTime(2)
count = count + 1
viztask.schedule(readline())
So what I did is two loops to read and write each line to a separate file ('marker.txt') as well as trying to continuously read in one second intervals. However, when I tested the program, it only reads up to the lines that were already present in the text file ('file_name.txt' or file1) right at the moment of initiation. Although it recognizes that there are new lines added (j increases) it doesn't read or write the lines.
Hope this is more understandable.
Thanks.
|