with open("target.txt", "r+") as f: d = f.readlines() f.seek(0) for i in d: if i != "line you want to remove...": f.write(i) f.truncate()