3

I am trying to use a pickle in Python and I am failing miserably. For each error that I overcome I get another message. I get the following message when I run my code. I am getting a syntax error for my use of 'else'. Why is that?

I am a newb to Python but I can't figure out what am I doing wrong?

import pickle

def main():
    file_mail = open('email.dat','wb')
    save_data(file_mail)
    file_mail.close()


def save_data(file):
    email = {}

    count = 0

    while count == 0:
        print('Add an email: 1')
        print('Modify an email: 2')
        print('Delete an email: 3')
        print('Display the list: 4\n')

        choice = input('Enter a number from the list above:')

        if int(choice)== 1:
            name = input('Name:')
            mail = input('E-mail:')
            email[name] = mail
            print('Added Successfully')

            if int(choice) == 2:
                name = input('Name:')
                mail = input('E-mail:')
                email[name] = mail
                print('Modified Successfully')

                if int(choice) == 3:
                    name = input('Name:')
                    mail = input('E-mail:')
                    email[name] = mail
                    print('Deleted Successfully')

                    if int(choice) == 4:
                        print(email)

                        else:
                            print('Invalid selection')
                            c = input('Do you want to continue y/n: ')

                            if c.upper() == 'N':
                                count = 1
                                print('Invalid Letter')
                                file_mail = open('email.dat','wb')
                                pickle.dump(email,file_mail)
                                file_mail.close()
main()
Part_Time_Nerd
  • 994
  • 7
  • 26
  • 56
  • 2
    When reading (at your 3rd line), you are opening your file with `'wb'` which means write binary mode. Change that to `'rb'` - read binary mode. – umutto Apr 04 '17 at 05:38

2 Answers2

4

Try to use rb mode when reading a pickle-containing file :

file_mail = open('email.dat','rb')
email = pickle.load(file_mail)

And use wb mode when writing a pickled representation of obj to the open file object:

output = open('data.pkl', 'wb')
pickle.dump(data1, output)

See more details from pickle example.

McGrady
  • 10,869
  • 13
  • 47
  • 69
  • That was my original but I got the error message email = pickle.load(file_mail) EOFError: Ran out of input – Part_Time_Nerd Apr 04 '17 at 05:41
  • @BWMustang13 you should check that the file is not empty, that's why you got the `EOFError` error. – McGrady Apr 04 '17 at 05:44
  • 1
    Have look at this answer [Why do I get “Pickle - EOFError: Ran out of input” reading an empty file?](http://stackoverflow.com/questions/24791987/why-do-i-get-pickle-eoferror-ran-out-of-input-reading-an-empty-file) – McGrady Apr 04 '17 at 05:46
  • I believe I ran it as the post suggested but I got the same error. – Part_Time_Nerd Apr 04 '17 at 06:03
  • I updated my question to incorporate a slightly different approach by segregating i into 2 separate functions. See my changes above, but I am getting an invalid syntax error with the else statement. Why is that? It might be because I have been looking at code the last 18 hours but it looks like the indentations should be correct. – Part_Time_Nerd Apr 04 '17 at 07:07
  • @BWMustang13 Just indentation error with the else statement. Check out `else` part from 43 to 52. – McGrady Apr 04 '17 at 07:23
0

Thanks @McGrady! Here is the final answer I got.

import pickle

def main():
    file_mail = open('email.dat','wb')
    save_data(file_mail)
    file_mail.close()


def save_data(file):
    email = {}

    count = 0

    while count == 0:
        print('Add an email: 1')
        print('Modify an email: 2')
        print('Delete an email: 3')
        print('Display the list: 4\n')

        choice = input('\nEnter a number from the list above:')

        if int(choice)== 1:
            name = input('Name:')
            mail = input('E-mail:')
            email[name] = mail
            print('Added Successfully\n')

        else:
            if int(choice) == 2:
                name = input('Name:')
                mail = input('E-mail:')
                if name in email:
                    email[name] = mail
                    print('Modified Successfully\n')
                else:
                    print('Name not found')

            else:
                if int(choice) == 3:
                    name = input('Enter name you want to delete:')
                    if name in email:
                        email.pop(name)
                        print('Deleted Successfully\n')
                    else:
                       print('Name not found') 
                else:
                    if int(choice) == 4:
                        print(email)

                    else:
                        print('Invalid selection\n')
                        c = input('Do you want to continue y/n: ')
                        if c.upper() == 'N':
                            count = 1
                        else:
                            if c.upper() == 'N':
                                count = 1
                                print('Invalid Letter')

            file_mail = open('email.dat','wb')
            pickle.dump(email,file_mail)
            file_mail.close()
main()
Part_Time_Nerd
  • 994
  • 7
  • 26
  • 56