return tail of a text log file

68 views
Skip to first unread message

Vin

unread,
Oct 14, 2014, 6:23:15 PM10/14/14
to web...@googlegroups.com
Hi all,

I need to stream a text log file via web2py. The file is generated by a python script that runs outside web2py, but is saved to the "uploads" folder.

This is what I have in a controller called 2.py:

def index():
    import os
    path=os.path.join(request.folder,'uploads','log.log')
    return response.stream(open(path,'rb'),chunk_size=4096)

When I navigate to ...app/2 the contents of log.log are displayed on the browser, yes. But my problem is that I only need to see the tail of the file (it can get very large), like you would from the command line.

tail -f log.log

Any ideas?

Thanks!
Vin

Dave S

unread,
Oct 15, 2014, 1:43:23 PM10/15/14
to web...@googlegroups.com

Python's seek() has an option to specify "relative to the end of file" for a file open in read mode.
Old docs: <http://docs.python.org/2.4/lib/bltin-file-objects.html>
Current docs: <http://docs.python.org/2/library/stdtypes.html#file-objects>

There is also os.stat()
<http://docs.python.org/2/library/os.html#files-and-directories>
 
/dps

Dave S

unread,
Oct 15, 2014, 2:44:21 PM10/15/14
to web...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages