[Python]如何取出一個超大文本文件的最后幾行
這是 python-中國 郵件列表中的一個問題。如果不考慮效率問題,這個問題可能很簡單,直接read出整個文件的內(nèi)容,然后split一下或者
還有更簡單的辦法。但是如果這個文件是“超大”,那么為了區(qū)區(qū)幾行而讀入整個文件有點‘“得不償失“。下面是我的實現(xiàn)。剛學(xué)python不久,也許沒有發(fā)
揮出它的優(yōu)點。
#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 |
|