67rt 发表于 2016-9-2 10:50:27

Python读取文件的最后一行(非空行)

利用Python读取文件(针对大文件和小文件两种)的首行(第一行)和末行(最后一行)。脚本借鉴了前人的两种处理思路(在下面的脚本中有注释说明引用出处),并修正了原先两种处理方法中如果文件末尾含有多个空行而返回空行的问题。

脚本内容可以从GitHub上获取:
https://github.com/DingGuodong/LinuxBashShellScriptForOps/blob/master/functions/file/getFileLastLine.py
脚本内容如下:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/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--
页: [1]
查看完整版本: Python读取文件的最后一行(非空行)