0

I am continuing on a program that I had written earlier, and am trying to save the data inputted in a separate file, so when I reopen the program, I keep the data.

addressbook = []
class Parent:
    def Create(self):
        create_contact = input('''Please input the details of the contact you would like to create.
                           ''')
        addressbook.append(create_contact)
        print("Successfully added")
        return
    def Remove_contact(self):
        remove_contact = input('''Please input the details of the contact you would like to remove.
                            ''')
        if remove_contact in addressbook:
            print(addressbook.remove(remove_contact))
            print("Successfully removed contact.")
        else:
            print("Error", search_contact, "not found in the list")
        return
    def Search(self):
        search_contact = input('''Please input the details of the contact you would like to search for.
                            ''')
        if search_contact in addressbook:
            print(search_contact, "found in Address Book")
        else:
            print("Error", search_contact, "not found in the list")
        return
    def Display_contacts(self):
        print("Displaying contacts....", addressbook)
        return

def menu(parent):
    menu = input('''Address book to store friends contact
   -------------------------------------------------------------
   -------------------------------------------------------------
   Select an option...
   1 - Add/Update contact...
   2 - Display all contacts...
   3 - Search...
   4 - Delete contact...
   5 - Quit
    ''')

    if menu == '1':
        parent.Create()

    elif menu == '2':
        parent.Display_contacts()

    elif menu == '3':
        parent.Search()

    elif menu == '4':
        parent.Remove_contact()

    elif menu == '5':
        print("Quitting program")
        quit()
    else:
        print("I did not understand that... Please try again")

p = Parent()
menu(p)

This is my old code, and am thinking about importing a file which contains a list, and adding to that list. Is that an appropriate method or is there and easier way? Thank you!

2
  • Add code to Write to a text file before quit() Commented Jul 24, 2021 at 9:23
  • You may find the atexit module useful for this docs.python.org/3/library/atexit.html Commented Jul 24, 2021 at 10:03

1 Answer 1

1

try the following code:

addressbook = []
class Parent:
    def Create(self):
        create_contact = input('''Please input the details of the contact you would like to create.
                           ''')
        addressbook.append(create_contact)
        print("Successfully added")
        return
    def Remove_contact(self):
        remove_contact = input('''Please input the details of the contact you would like to remove.
                            ''')
        if remove_contact in addressbook:
            print(addressbook.remove(remove_contact))
            print("Successfully removed contact.")
        else:
            print("Error", search_contact, "not found in the list")
        return
    def Search(self):
        search_contact = input('''Please input the details of the contact you would like to search for.
                            ''')
        if search_contact in addressbook:
            print(search_contact, "found in Address Book")
        else:
            print("Error", search_contact, "not found in the list")
        return
    def Display_contacts(self):
        print("Displaying contacts....", addressbook)
        return

def writeFile():
    with open("contact.txt",'a') as file:
        for contact in addressbook:
            file.write(str(contact)+"\n")

def loadFileContacts():
    with open("contact.txt",'r') as file:
        for contact in file:
            addressbook.append(contact)

def menu(parent):
    menu = input('''Address book to store friends contact
   -------------------------------------------------------------
   -------------------------------------------------------------
   Select an option...
   1 - Add/Update contact...
   2 - Display all contacts...
   3 - Search...
   4 - Delete contact...
   5 - Quit
    ''')

    if menu == '1':
        parent.Create()

    elif menu == '2':
        parent.Display_contacts()

    elif menu == '3':
        parent.Search()

    elif menu == '4':
        parent.Remove_contact()

    elif menu == '5':
        print("Quitting program")
        writeFile()
        quit()
    else:
        print("I did not understand that... Please try again")


p = Parent()
loadFileContacts()
print(addressbook)
while True:
    menu(p)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.