12345678910111213141516171819 |
- def tail(file, n=1, bs=1024):
- f = open(file)
- f.seek(0,2)
- l = 1-f.read(1).count('\n')
- B = f.tell()
- while n >= l and B > 0:
- block = min(bs, B)
- B -= block
- f.seek(B, 0)
- l += f.read(block).count('\n')
- f.seek(B, 0)
- l = min(l,n)
- lines = f.readlines()[-l:]
- f.close()
- return lines
-
- lines = tail("SampleTextFile_10kb.txt", 2)
- for line in lines:
- print (line)
|