newbie: Some file object tips?

Flavian Hardcastle deathtospam43423 at altavista.com
Thu Feb 21 09:50:59 EST 2002


Thanx! This little code has been immensely useful to me. I've learned a lot 
from it.

I'm currently thinking of using parts of it. Instead of actually scrolling 
back from the end of the log, and counting markers indicating the spaces 
between posts ... I'm thinking of using a dictionary array to keep a 
database of the precise byte position of each chat post. I would determine 
this using seek(0,2) and tell() operands as you describe below. Does that 
sound like a better way of doing it?


Gustavo Cordova <gcordova at hebmex.com> wrote in 
news:mailman.1014133831.1330.python-list at python.org:
>> 
>> So does this command ....
>> 
>> > lines = open("log.txt").readlines()
>> 
>> ... actually load the whole log into RAM.
>> 
> 
> Yes it does.
> 
> But then, what's actually flawed is the algorithm,
> not the program, or the tools.
> 
> You need a new way to store the message which will
> let you read only part of the log into memory.
> Right?
> 
> Well, sorry. Maybe "flawed" is a too harsh qualification,
> but overly simple. Ok.
> 
> Hmmm... What would I do?
> 
> I wouldn't read the "last 'n' lines", as you put it,
> but something easier to calculate, "the last 'n' K of text",
> which is more "file-like".
> 
> So, to read the lines in the last "n" Kb of text:
> 
> ====== SOME CODE ======
> def GetLog(filename, maxSize=None):
>      log = open(filename)
>      if maxSize:
>           # Seek to end of file.
>           log.seek(0,2)
> 
>           # Obtain current position index.
>           fileLen = log.tell()
> 
>           # If file is larger than maxSize,
>           # seek to (EOF - maxSize).
>           if maxSize < fileLen:
>                log.seek(fileLen-maxSize)
> 
>                # Skip one line.
>                log.readline()
> 
>           # Or return to the file beginning.
>           else:
>                log.seek(0)
> 
>      # Return all lines.
>      return log.readlines()
> ====== SOME CODE ======
> 
> So now you only get the last maxSize Kb lines-worth of
> lines from the log file.
> 
> Maybe it's a little clumsy, I bet someone can clean it
> up a bit.
> 
> Good luck :-)
> 
> -gustavo
> 
> 



-- 
"You can tell whether a man is clever by his answers. You can tell whether 
a man is wise by his questions."  Naguib Mahfouz

netvegetable at dingoblue.net.au



More information about the Python-list mailing list