這是 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