Interview Topics:
1. Use Python to implement TAIL-F function, display the last 15 lines by default, and output new lines in real time?
Problem Solving Ideas:
1. This requirement has been encountered in many scenarios, and has been discussed in various groups, although there are ready-made templates such as pyinotify and other modules to achieve, but the interview would like to use your problem-solving ideas to judge the interview, specific to tail need to achieve 2 functions, a real-time output new content, A default output of the first 15 lines, the former directly loop open file read, the global variable record the last read position, the next loop seek to the last location read, and for the default display of the first 15 rows is assumed a row of 1000 bytes, loop read, When the total length of the file is less than 1000, then read the split line from the beginning to take out the 10 lines, even if not enough 10 lines, when the total length of the file is greater than or equal to 1000, if the partition is greater than 10 rows, then read the split row after the 10 row is removed , If less than 10 lines continue to 1000 reads, and so forth ~
Specific implementation:
#!/usr/bin/env python# -*- coding: utf-8 -*-"" "## authors: limanman# 51ctobg: http://xmdevops.blog.51cto.com/# purpose:# "" "From __future__ import absolute _import# Description: Import Public module import os import sysimport timeimport chardet# Description: Import other modules # if __name__ == ' __main__ ': seekps = 0; target = ' Data.txt '; try: # the pointer jumps to the end as the read position with open ( target, ' r+b ') as f: F.seek (0, os. Seek_end) seekps = f.tell () print ' notice: file Length is ', seekps rest_lines = [] # read 15 rows by default line_reads = 15 # Assuming default of 1000 bytes per line line_bytes = 1000 ' 1. when f_length < line_bytes, the seek to 0 began to read, read break 2. when F_length > line_bytes, the number of newline characters n means n+1 rows => when N+1 >= line_reads, remove the first line_reads elements => when N+1 < line_reads, Go ahead seek to count*line_reads position read ' count = 1 while True: if seekps <= line_bytes*count: f.seek (0) rest_lines = f.read (). Split (OS.LINESEP) [-line_reads:] Break &nbSp; f.seek ( -1*line_bytes*count, 2) rest_lines = f.read (). Split (OS.LINESEP) if len (Rest_lines) >=line_reads: rest_lines = rest_lines[- line_reads:] break else: count += 1 for line in rest_lines: code = chardet.detect (line). Get (' encoding ') line = line.decode (code). Encode ( sys.stdout.encoding) sys.stdout.write (". Join" ([Line.strip (), os.linesep])) sys.stdout.flush () except exception, e: print ' Notice: open file with error ({0}) '. Format (e) exit () print ' Notice: start position is ', seekps while true: try: with open (Target, ' r+b ') as f: f.seek (0, os. Seek_end) # Prevent accidental truncation of data if f.tell () <seekps: f.seek (F.tell ()) else: f.seek (SEEKPS) while true: line = f.readline () if not line.strip (): break code = chardet.detect (line). Get (' encoding ') line = line.decode (code). Encode (sys.stdout.encoding) sys.stdout.write (". Join" ([Line.strip (), os.linesep])) &nbsP; sys.stdout.flush () seekps = f.tell ( ) except Exception, e: print ' Notice: open file with error ({0}) '. Format (e) break time.sleep (0.1)
There are pictures like:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/8D/4E/wKioL1iXNFngMPcWAABFkWpclw8083.png "title=" Xxoo.png "alt=" Wkiol1ixnfngmpcwaabfkwpclw8083.png "/>
This article is from the "Li-Yun Development Road" blog, please be sure to keep this source http://xmdevops.blog.51cto.com/11144840/1895119
Interview _python. Operation and maintenance development. 0004. Use Python to implement tail real-time output new log?