利用Python读取文件(针对大文件和小文件两种)的首行(第一行)和末行(最后一行)。脚本借鉴了前人的两种处理思路(在下面的脚本中有注释说明引用出处),并修正了原先两种处理方法中如果文件末尾含有多个空行而返回空行的问题。
脚本内容可以从GitHub上获取:
脚本内容如下:
#!/usr/bin/python # encoding: utf-8 # -*- coding: utf8 -*- """ Created by PyCharm. File: LinuxBashShellScriptForOps:getFileLastLine.py User: Guodong Create Date: 2016/9/1 Create Time: 11:05 """ import os # Refer: http://www.pythonclub.org/python-files/last-line def get_last_line(inputfile): filesize = os.path.getsize(inputfile) blocksize = 1024 dat_file = open(inputfile, ‘rb‘) last_line = "" if filesize > blocksize: maxseekpoint = (filesize // blocksize) dat_file.seek((maxseekpoint - 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_line # Refer: http://code.activestate.com/recipes/578095/ 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 return headers, last_line # My Implementation def get_file_last_line(inputfile): filesize = os.path.getsize(inputfile) blocksize = 1024 with open(inputfile, ‘rb‘) as f: last_line = "" if filesize > blocksize: maxseekpoint = (filesize // blocksize) f.seek((maxseekpoint - 1) * blocksize) elif filesize: f.seek(0, 0) lines = f.readlines() if lines: lineno = 1 while last_line == "": last_line = lines[-lineno].strip() lineno += 1 return last_line # Test purpose # print get_last_line(os.path.abspath(__file__)) # print print_first_last_line(os.path.abspath(__file__)) # print get_file_last_line(os.path.abspath(__file__))
--end--
本文出自 “通信,我的最爱” 博客,请务必保留此出处http://dgd2010.blog.51cto.com/1539422/1845139
原文地址:http://dgd2010.blog.51cto.com/1539422/1845139