inRecordingMode = False for line in file: if not inRecordingMode: if line.startswith('<START>'): inRecordingMode = True elif line.startswith('<END>'): inRecordingMode = False else: yield line