Welcome, guest | Sign In | My Account | Store | Cart

First line of the file may have the header information. Getting last line from the file is a common requirement in the case of log files. How we can do that without reading the entire file to memory and doing some operation on that. The script will take filename as argument where that file is placed in the same path of the script.

Python, 25 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import os
import sys

def print_first_last_line(inputfile) :
    filesize = os.path.getsize(inputfile)
    blocksize = 1024
    dat_file = open(inputfile, 'rb')
    headers = dat_file.readline().strip()
    if filesize > blocksize :
        maxseekpoint = (filesize // blocksize)
        dat_file.seek(maxseekpoint*blocksize)
    elif filesize :
        maxseekpoint = blocksize % filesize
        dat_file.seek(maxseekpoint)    
    lines =  dat_file.readlines()    
    if lines :
        last_line = lines[-1].strip()
    print "first line : ", headers
    print "last line : ", last_line

if __name__ == "__main__" :
    if len(sys.argv) >= 2:
        print_first_last_line(sys.argv[1])
    else:
        sys.exit("Usage %s filename" % sys.argv[0])

1 comment

dongzh 12 years ago  # | flag

After reading this , want to ask : why you set the blocksize=1024 here, was it the filesystem blocksize such as output of: tune2fs -l /dev/sda{num} ? or some other indicator ?

Created by Dileep D S on Wed, 4 Apr 2012 (MIT)
Python recipes (4591)
Dileep D S's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks