f.close()
for i, line in enumerate(searchlines):
    if 'searchphrase' in line:
        for l in searchlines[i:i + 3]:
            print(l, end=' ')
print()
Search a text file and print related lines in Python?