diving into python

I decided Mark Pilgrim’s “further reading” program was intriguing enough to take a whack at building one.

To add to the fun, why not do it in python?

So the most basic step is to look at the logfile and pull out referers (yes, it’s spelt wrong but it looks right: usage and habit).

To that end, I found a script that purports to do what tail -f does.
ASPN : Python Cookbook : tail -f in Python

Description:A simple implementation of the standard UNIX utility tail -f in Python.


import time
while 1:
where = file.tell()
line = file.readline()
if not line:
time.sleep(1)
file.seek(where)
else:
print line, # already has newline

Looks simple, alright. It doesn’t work.

For one thing, there’s nothing to open a file in there. file = open(“/usr/local/weblogs/httpd-access.log”) would be useful. And we need to do that outside the while block: we open the file, seek to the end, then wait for new stuff to appear (for while not line to be false), then print or do whatever to what we find there.

I’m still trying to figure it out: what seems most worth figuring out is how to delimit a file on newlines instead of by characters since I am going to examine the file line by line.

When will I ever learn that most of the “helpful” scripts and code fragments rarely are?