import sys def is_hamlet_in_line(line): if 'Hamlet' in line: return True else: return False file1 = open("shakespeare.res", "a") for line in sys.stdin: if is_hamlet_in_line(line): file1.write(line) print(line.rstrip('\n'))