Zenek
Zenek

Reputation: 120

Retrieving information from text file

I would like to take in the variable "name" the input from the user, then I would like load the account.zk (that is a normal text file) in to a list, and then I would like check if the input is already on this list. If is not, I would like to add it, and on the contrary, I would like to skip and go ahead.

I've coded this function myself, but I didn't succeed! Can someone understand why?

# 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

Upvotes: 0

Views: 39

Answers (1)

vvvvv
vvvvv

Reputation: 31599

I think what you want to do is the following:

# 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()

Upvotes: 1

Related Questions