Python Forum
Text file not being created
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Text file not being created
#1
I'm stuck. I have no idea what I did wrong but, basically, I need to create a temporary text file or something? Something to save the emails after I end to program so I can come back to it and look up the email. Here's the description of the homework.
Quote:Write a program that keeps names and addresses in a dictionary as key-value pairs. The program should display a menu that lets the user look up a person's email address, add a new name and email address, change an existing email address, and delete an existing name and email address. Each time the program starts, it should retrieve the dictionary from the file and unpickle it.
"""
nameandemail.py uses a penu system to maintain and manage email adresses
author: Inge Teleky
2/24/20
"""

import pickle


def main():
    filename = input("Enter your email file name: ")
    emails = load_emails(filename)

    choice = get_choice()

    while choice != 5:
        if choice == 1:
            look_up(emails)
        elif choice == 2:
            add(emails)
        elif choice == 3:
            change(emails)
        elif choice == 4:
            delete(emails)
        elif choice == 5:
            save_emails(filename)
            print("Information saved.")
        else:
            print("Invalid choice. Please enter 1-5 for your choice.")

        choice = get_choice()


def load_emails(filename):
    """
    read name and email into a dictionary
    :param filename: binary file than contains name and email address
    :return:
    """
    try:
        input_file = open(filename, 'rb')
        #unpickle the file
        email_dictionary = pickle.load(input_file)
        input_file.close()
    except IOError:
        email_dictionary = {}

    return email_dictionary


def get_choice():
    print("Email Book Management")
    print("1. Look up an email.")
    print("2. Add a new name and email.")
    print("3. Change an existing email.")
    print("4. Delete a name and email.")
    print("5. Quit the program")
    choice = int(input("Enter your choice, 1-5: "))
    return choice


def add(emails):
    """

    :param emails: dictionary of naame and email
    :return: updated emails and dictionary
    """
    #ask user for name and email
    name = input("Enter name: ")
    email = input("Enter email: ")
    if name not in emails:
        emails[name] = email
    else:
        print("The name already exists")

    return emails


def look_up(emails):
    name=input("Enter name: ")
    if name in emails:
        print(name, ": ", emails[name])
    else:
        print("The specified name does not exist.")


def change(emails):
    name = input("Enter the name of the email you wish to change: ")
    if name in emails:
        change = input("What do you wish to change the email to? ")
        emails[name] = change

    return emails


def delete(emails):
    name = input("Enter the name of the email you wish to delete. ")
    if name in emails:
        delete = input("Are you sure you wish to delete this email? Y or y for yes. Email name: " + name)
        if delete == 'y' or delete == 'Y':
            emails.pop(name)
        else:
            pass

    return emails


def save_emails(filename):
    with open(filename) as name:
        pickle.dump(filename, name, pickle.HIGHEST_PROTOCOL)





main()
Reply
#2
That looks nice. But what is your question? What is going wrong?
Indeed the data is stored in a dictionary with the name "emails". When you end the program this dictionary should be saved to a file with pickle and when you start it again the data should be read from this file.
I see only two problems.
The first time you start the program there will be no file. So the file has to be created with an empty dictionary.
Then in main() you call the function load_emails() which is defined later. Perhaps you should put main() near the end of the program.
Reply
#3
I think problem lies in the save function. You need to think about what you are going to save and where. Since file is not being created, you need to ask yourselves which mode you are opening your file and what are you going to store in it.

Hope this hint helps in your homework.
Reply
#4
while choice != 5:     # means that will not enter into while loop when choice is 5
/.../
    elif choice == 5:  # as choice can't be 5 it will never run
            save_emails(filename)
            print("Information saved.")
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert text from an image to a text file Evil_Patrick 5 4,311 Jul-30-2019, 07:57 PM
Last Post: DeaD_EyE
  reading text file and writing to an output file precedded by line numbers kannan 7 10,469 Dec-11-2018, 02:19 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020