I’m needing to output the contents of a log file in a window similar to
what “tail -f” does in the Unix shell. Any recommendations from the
gentle readership here would be appreciated.
Hi James,
I'm needing to output the contents of a log file in a window similar to what
"tail -f" does in the Unix shell. Any recommendations from the gentle
readership here would be appreciated.
I adapted this Python script:
import time, os
def tail(fileName):
file = open(filename,'r')
# Find the size of the file and move to the end
st_results = os.stat(filename)
st_size = st_results[6]
file.seek(st_size)
while 1:
where = file.tell()
line = file.readline()
if not line:
time.sleep(1)
file.seek(where)
else:
print line, # already has newline
to work with wxPython (i.e., putting the text in a window). Obviously,
you can not freeze the GUI with the while loop, so I just implemented
the tail method using the threading module and, every time new text
appears, I just post an event to the window which updates its text.
Andrea.
"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.virgilio.it/infinity77/
···
On 6/5/07, James Hartley wrote:
James Hartley wrote:
I'm needing to output the contents of a log file in a window similar to what "tail -f" does in the Unix shell. Any recommendations from the gentle readership here would be appreciated.
You can use a worker thread to watch the file and read new lines as they appear in the file, and then send those lines with wx.CallAfter or some other thread-safe mechanism to the UI widget that will display them. If you're on a unix system then you can use the select module to know when the file has more data available than what you've read so far. On Windows there are equivalent things in the windows API which you can probably get to with PyWin or ctypes, but I'm not familiar with those APIs at all.
···
--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!
Wouldn’t be simpler to just monitor the size of the file via os.stat().st_size and read the difference from the old position to the tail using something like seek… inside a wxTimer event handler set-up to fire every second or so?
:o)
···
On 6/5/07, Robin Dunn robin@alldunn.com wrote:
If you’re on a unix system then you can use the select module to know when
the file has more data available than what you’ve read so far.
–
There is NO FATE, we are the creators.
Peter Damoc wrote:
If you're on a unix system then you can use the select module to
know when
the file has more data available than what you've read so far.Wouldn't be simpler to just monitor the size of the file via
os.stat().st_size and read the difference from the old position to the
tail using something like seek.... inside a wxTimer event handler set-up
to fire every second or so?
The difference between select and a one-second-poll is:
* select returns immediately when data is avaialable,
so on average it will be half a second faster.
* select does not return if there is no data avaialable,
so you do not waste any CPU time for nothing.
Of course, if waiting up to one second too long does
not matter much to you, your approach will be fine, too.
Carsten.
···
On 6/5/07, *Robin Dunn* <robin@alldunn.com <mailto:robin@alldunn.com>> > wrote: