from pathlib import Path txt1 = Path('E:/') / "basic.txt" txt2 = Path('E:/') / "bad.txt" with txt1.open("r", encoding="utf-8") as f: titles1 = [line.strip() for line in f if line.strip()] with txt2.open("r", encoding="utf-8") as f: titles2 = [line.strip() for line in f if line.strip()] result = [] for title in titles1: if title not in titles2: result.append(title) txt3 = Path('E:/') / "good.txt" with txt3.open("r", encoding="utf-8") as f: titles3 = [line.strip() for line in f if line.strip()] result_new = [] for title in result: if title in titles3: result_new.append(title) print(result_new)