从文本文件中检索信息

Retrieving information from text file

我想接受用户输入的变量“name”,然后我想将 account.zk(这是一个普通的文本文件)加载到列表中,然后我会比如检查输入是否已经在这个列表中。如果不是,我想补充,反之,我想跳过并继续。

这个功能我自己写过,但是没有成功!有人能明白为什么吗?

# Start Downloading System
name = input(Fore.WHITE+"Enter Profile Name To Download: ")

# Save Account (used for check the updates!)
file_object = open("account.zk", "r")

for line in file_object:
  stripped_line = line.strip()
  line_list = stripped_line.split()
  list_of_lists.append(line_list)


if (len(list_of_lists) != len(set(name))):
    print("\n\nAlready in List!\n\n")
    file_object.close
    time.sleep(5)
else:
    file_object.close
    file_object = open("account.zk", "w")
    file_object.write(name + "\n")
    file_object.close

我想你想做的是:

# Start Downloading System
name = input(Fore.WHITE + "Enter Profile Name To Download: ")

# Save Account (used for check the updates!)
file_object = open("account.zk", "r")

list_of_lists = []
for line in file_object:
  stripped_line = line.strip()
  line_list = stripped_line.split()
  list_of_lists.extend(line_list)  # add the elements of the line to the list

if name in list_of_lists:  # check if the name is already in the file
    print("\n\nAlready in List!\n\n")
    file_object.close()
    time.sleep(5)
else:  # if not, add it
    file_object.close()
    file_object = open("account.zk", "w")
    file_object.write(name + "\n")
    file_object.close()