WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 06-10-2009, 12:10 PM
Woosuk Jang Woosuk Jang is offline
Member
 
Join Date: Jun 2009
Posts: 8
read method needed

Is there a method where I can continuously read data from a text file (such that, when a new data is written in that file while program's running, it reads that immediately) ?
Reply With Quote
  #2  
Old 06-12-2009, 03:04 PM
Jeff Jeff is offline
WorldViz Team Member
 
Join Date: Aug 2008
Posts: 2,471
The methods for reading and writing to files can be found in the Python documentation. What's the reason for reading it immediately after writing it to file?
Reply With Quote
  #3  
Old 06-16-2009, 08:31 AM
Woosuk Jang Woosuk Jang is offline
Member
 
Join Date: Jun 2009
Posts: 8
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.
Reply With Quote
  #4  
Old 06-22-2009, 05:49 PM
Jeff Jeff is offline
WorldViz Team Member
 
Join Date: Aug 2008
Posts: 2,471
Please use code tags when posting code next time. Adding the following made it work for me.
Code:
line = linecache.getline(file1,i)
linecache.clearcache()
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
parallel port method bionic1 Vizard 2 05-04-2009 06:41 PM
read from SP Moh200jo Vizard 1 02-19-2009 10:36 AM
Read file....many issues Uttama_vizard Vizard 9 07-08-2008 02:16 PM
mapping method FlyingFish Vizard 1 07-12-2007 09:06 AM
method of a class instance not accepted as Callback Gilliard Vizard 1 08-10-2005 05:49 PM


All times are GMT -7. The time now is 12:15 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC