这是 python-中国 邮件列表中的一个问题。如果不考虑效率问题,这个问题可能很简单,直接read出整个文件的内容,然后split一下或者还有更简单的办法。但是如果这个文件是“超大”,那么为了区区几行而读入整个文件有点‘“得不偿失“。下面是我的实现。刚学python不久,也许没有发挥出它的优点。
#last lines
#by Kevin Yuan
def last_lines(filename, lines = 1):
#print the last line(s) of a text file
"""
Argument filename is the name of the file to print.
Argument lines is the number of lines to print from last.
"""
block_size = 1024
block = ''
nl_count = 0
start = 0
fsock = file(filename, 'rU')
try:
#seek to end
fsock.seek(0, 2)
#get seek position
curpos = fsock.tell()
while(curpos > 0): #while not BOF
#seek ahead block_size+the length of last read block
curpos -= (block_size + len(block));
if curpos < 0: curpos = 0
fsock.seek(curpos)
#read to end
block = fsock.read()
nl_count = block.count('/n')
#if read enough(more)
if nl_count >= lines: break
#get the exact start position
for n in range(nl_count-lines+1):
start = block.find('/n', start)+1
finally:
fsock.close()
#print it out
print block[start:]
if __name__ == '__main__':
import sys
last_lines(sys.argv[0], 5) #print the last 5 lines of THIS file