newbie: Some file object tips?

Gustavo Cordova gcordova at
Tue Feb 19 10:37:22 EST 2002

> 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.

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.,2)

		# Obtain current position index.
		fileLen = log.tell()

		# If file is larger than maxSize,
		# seek to (EOF - maxSize).
		if maxSize < fileLen:

			# Skip one line.

		# Or return to the file beginning.

	# 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 :-)


More information about the Python-list mailing list