Files
  • main.py
  • data.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
search = "convallis"  # some substring we're searching for
end = "END"

with open('data.txt', 'r') as f:
  it = iter(f)  # explicitly create an iterator
  # advance the iterator to the start position
  next(line for line in it if search in line.lower())  

  # now, iterating will start from where we left off above, and all we
  # have to track is when to end the iteration by checking for the end substring
  for line in it:
    if end in line:  # no lower() here because we're quite sure that the end sentinel is *exactly* "END"
      break
    else:
      print(line)