Jan-31-2023, 09:29 PM
(This post was last modified: Jan-31-2023, 09:29 PM by deanhystad.)
Your logic is backward. You should present results when you encounter a start, not an end. It is omly when you see START, or run out of lines, that you know you found the last end.
with open('data.txt', 'r') as file: start = end = None for line in map(str.strip, file): if 'TEST Started' in line: if end: # Print previous start/end pair print(start, end, sep="\n", end="\n\n") start = line end = None elif 'TEST end' in line: end = line if start and end: print(start, end, sep="\n")