code example for Python to read n rows after a file

Source: Internet
Author: User
This article mainly introduces the Python implementation of the last n read the file of the method, involving Python for file reading, traversal and operation related operations skills, the need for friends can refer to the following

#-*-Coding:utf8-*-import osimport timeimport datetimeimport mathimport stringdef get_last_line (inputfile): FileSize = O S.path.getsize (inputfile) blocksize = 1024x768 Dat_file = open (Inputfile, ' r ') last_line = "" lines = Dat_file.readlines () cou NT = len (lines) if count>60:   num=60 Else:   num=count i=1; lastre = [] for I in range (1, (num+1)):   If lines: C3/>n =-     I last_line = Lines[n].strip ()     #print "last line:", Last_line     dat_file.close ()     #print i     Lastre.append (last_line) return lastre# Gets the result of the last row re = Get_last_line ('.. /update/log/rtime/rtime20130805.log ') print Len (re) for N in Re:  strlist = N.split ("  )  if strlist[1] = = ' Ok ' and String.atoi (Strlist[2]) >1000:     print ' data bar count normal '     print ' OK '  else:     print ' too little data to check email '

The above processing and log file formats are

2013-08-05 16:09:30  OK  16732013-08-05 16:10:34  ok  16282013-08-05 16:11:55  OK  712013-08-05 16:13:02  OK  14412013-08-05 16:14:06  ok  16342013-08-05 16:15:10  OK  17172013-08-05 16:16:14  OK  16872013-08-05 16:17:18  ok  16422013-08-05 16:18:27  OK  16552013-08-05 16:19:33  OK  1655

Read the last line:

#返回文件最后一行函数def Get_last_line (inputfile): FileSize = Os.path.getsize (inputfile) blocksize = 1024x768 Dat_file = open (inputfil E, ' r ') Last_line = "" If FileSize > blocksize:   maxseekpoint = (filesize//BlockSize)   Dat_file.seek ((maxsee kpoint-1) *blocksize) elif filesize:   #maxseekpoint = blocksize% filesize   dat_file.seek (0, 0) lines = dat_file. ReadLines () If lines:   last_line = Lines[-1].strip () #print "last line:", Last_line dat_file.close () return Last_li Ne

"Recommended"

1. Python Free video tutorial

2. Python's application in Data science

3. Python Learning manual

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.