Most efficient way to search the last x lines of a file in python
Posted
by Harley
on Stack Overflow
See other posts from Stack Overflow
or by Harley
Published on 2008-11-03T23:01:50Z
Indexed on
2010/03/12
23:37 UTC
Read the original article
Hit count: 352
I have a file and I don't know how big it's going to be (it could be quite large, but the size will vary greatly). I want to search the last 10 lines or so to see if any of them match a string. I need to do this as quickly and efficiently as possible and was wondering if there's anything better than:
s = "foo"
last_bit = fileObj.readlines()[-10:]
for line in last_bit:
if line == s:
print "FOUND"
© Stack Overflow or respective owner