Python读取文件的最后一行(非空行)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python读取文件的最后一行(非空行)相关的知识,希望对你有一定的参考价值。
利用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
以上是关于Python读取文件的最后一行(非空行)的主要内容,如果未能解决你的问题,请参考以下文章
Java读取Excel中数据时怎么判断Excel中的最后一行
java I/O 读取一个txt文件中每一个空行之后的第一行,怎么办?