I have a file with about 50 blocks of 14 lines, with each block starting with certain string that I can search for. Since the file is otherwise fairly large, I am using the enumerate function as:
def search_and_return_a_block(l_no,file_path, string):
with open(file_path, 'r') as file:
for l_no, line in enumerate(file):
if string in line:
#append lines into a list of 14 lines
break
return list,l_no
Now, I want only the top 2 blocks out of the 50, so when I call this function, I wish to start searching from the bottom of the 1st block and not from the beginning of the file. The function does return correct line number the first time.
However, the above for loop always start from 0 and ignores the value of its first argument (l_no) I call it with. This causes it to print only the first block 2 times and not move onto the next block.
I also tried to use the optional argument of the enumerate function with no success. Is there an elegant way to do this?