Created
August 24, 2016 01:59
-
-
Save fengxuway/c8b057313b3013f4281731637563efd2 to your computer and use it in GitHub Desktop.
倒读文件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os, re, sys, time | |
def filerev(somefile, buffer=256): | |
somefile.seek(0, os.SEEK_END) | |
size = somefile.tell() | |
lines = [''] | |
rem = size % buffer | |
pos = max(0, (size // buffer - 1) * buffer) | |
while pos >= 0: | |
somefile.seek(pos, os.SEEK_SET) | |
data = somefile.read(rem + buffer) + lines[0] | |
rem = 0 | |
pos -= buffer | |
lines = re.findall(r'[^\n]*\n?', data) | |
ix = len(lines) - 2 | |
while ix > 0: | |
yield lines[ix] | |
ix -= 1 | |
else: | |
yield lines[0] | |
def main(): | |
st = time.time() | |
with open('/Users/fengxu/Downloads/SecureCRT/log_201608051100/dev/shm/mqtt/red5.log', 'r') as f: | |
for line in filerev(f): | |
sys.stdout.write(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment