inp = open('ips.txt', 'r') uniqlines = list(dict.fromkeys(inp.readlines())) inp.close() out = open('ips.txt', 'w') out.writelines(uniqlines) out.truncate(out.tell()-1) out.close()