首页 > 其他 > 详细

读取文件最后一行的两种方式

时间:2016-06-08 06:49:35      阅读:236      评论:0      收藏:0      [点我收藏+]

 

 

‘‘‘读取文件最后一行‘‘‘

import os

# 小文件:批量读取
def get_last_line(filename=mark.csv):
    fullfilename = os.path.join(os.path.dirname(__file__), filename)
    with open(fullfilename, r, encoding=utf-8) as f:
        lines = f.readlines() # 批量
        lastline = lines[-1]
    return lastline
    
# 大文件:逐行读取
def get_last_line2(filename=mark.csv):
    fullfilename = os.path.join(os.path.dirname(__file__), filename)
    with open(fullfilename, r, encoding=utf-8) as f:
        lastline = f.readline() # 第一行
        while lastline:
            line = f.readline() # 逐行
            if not line: break
            lastline = line
    return lastline


if __name__ == __main__:
    print(get_last_line())
    #print(get_last_line2())

 

读取文件最后一行的两种方式

原文:http://www.cnblogs.com/hhh5460/p/5568870.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!