#!/usr/bin/env python
# -*-mode: python; coding: iso-8859-1 -*-
#
# Copyright (c) Peter Astrand <astrand@cendio.se>
import os
import string
class BackwardsReader:
"""Read a file line by line, backwards"""
BLKSIZE = 4096
def readline(self):
while 1:
newline_pos = string.rfind(self.buf, "\n")
pos = self.file.tell()
if newline_pos != -1:
# Found a newline
line = self.buf[newline_pos+1:]
self.buf = self.buf[:newline_pos]
if pos != 0 or newline_pos != 0 or self.trailing_newline:
line += "\n"
return line
else:
if pos == 0:
# Start-of-file
return ""
else:
# Need to fill buffer
toread = min(self.BLKSIZE, pos)
self.file.seek(-toread, 1)
self.buf = self.file.read(toread) + self.buf
self.file.seek(-toread, 1)
if pos - toread == 0:
self.buf = "\n" + self.buf
def __init__(self, file):
self.file = file
self.buf = ""
self.file.seek(-1, 2)
self.trailing_newline = 0
lastchar = self.file.read(1)
if lastchar == "\n":
self.trailing_newline = 1
self.file.seek(-1, 2)
if __name__=="__main__":
br = BackwardsReader(open(‘txt.txt‘))
for i in range(14):
print br.readline()
相关文章
- 07-10一个linux下socket编程的例子,client连server
- 07-10Linux 环境下安装配置 TigerVNC Server 并启用当前会话远程服务(X0VNC)
- 07-10Linux下SVN server 的使用及权限配置
- 07-10VNC CentOS Linux下VNC Server远程桌面配置详解
- 07-10CentOS Linux下VNC Server远程桌面配置详解
- 07-10(总结)CentOS Linux下VNC Server远程桌面配置详解
- 07-10Linux系统下安装配置 OpenLDAP + phpLDAPadmin
- 07-10linux 下连接 sql server 数据库遇到了错误:error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol
- 07-10Linux下Instant Client的安装与配置
- 07-10linux环境下oracle client安装和配置