无法设置列表 python

not able to set a list python

我有一个用户名列表。只应添加文本文件中不存在的用户名。问题是已经存在的用户名正在被添加。我正在打印列表以确保其正常工作。出现此错误:重新声明了上面定义的 'myList',但未使用。

myList = []

with open('list.txt', 'r') as file:
    myList = file.readlines()

print(myList)

username = input("Enter username: ")

if username not in myList:
    with open("list.txt", 'a+') as output:
        output.write(str(username) + "\n")
    print(f"Successfully added {username} to list")
else:
    print(f"{username} already exists in list")

         

我很确定这条线

myList = file.readlines() 每行返回 \n

所以改成

myList = [line.rstrip() for line in file.readlines()]