I'm looking for a string str in a file and need the line after the occurrence of str. Right now, I am opening a file, reading all lines, saving them to a list and then looking up for that string in a list. Here's my code:
with open(file,'r') as file1:
data = file1.readlines()
for num, line in enumerate(data,1):
if 'str' in line:
print line, data[num+1]
I know that str will be somewhere in the last 10 lines in file. So, I want to read and save the last 10 lines as a list rather than the whole file in the data variable.
I don't want to do $tail file.dat > file1.dat and go from there.
I want something like reversed(readlines()) and save 10 lines to a list. How do I do it?