Welcome, guest | Sign In | My Account | Store | Cart

Utility to output file from backward. Useful for logs output.

Python, 28 lines
 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
#!/usr/bin/python
import sys
import os.path
import os

if len(sys.argv) < 2:
    print('Usage: %s file_name' % sys.argv[0])

fp = open(sys.argv[1],'r')
#size = os.path.getsize(sys.argv[1]) 
fp.seek(-1, 2)  

try:
    data = ''
    data = fp.read(1)
    while True:
        fp.seek(-2, 1)
        t = fp.read(1)
        if t == '\n':
            print data
            data = ''
        else:
            data = t+data
except Exception, e:
    print data
    sys.exit(0)

print data
Created by Andrey Nikishaev on Tue, 17 May 2011 (MIT)
Python recipes (4591)
Andrey Nikishaev's recipes (7)

Required Modules

  • (none specified)

Other Information and Tasks