[Tutor] How to I make it save the data properly? (was Re: How do I fix this ValueError?)

Nathan Pinno falcon3166 at hotmail.com
Wed Aug 10 03:46:36 CEST 2005


I tried fixing it. Here is the new code:

#This is for a password protected program to store passwords.
import getpass
password = "hello"
sitelist = {}

def load_file(pw):
    import os
    filename = 'passcard.txt'
    if os.path.exists(filename):
       store = open(filename,'r')
       for line in store:
          site = line.strip()
          ID = line.strip().strip()
          pw[site] = ID
    else:
        store = open(filename,'w') # create new empty file
    store.close()

def save_file(pw):
    store = open('passcard.txt',"w")
    for site,ID in sitelist.items():
        store.write(site + '\n')
        store.write(ID + '\n')
    store.close()


def main_menu():
    print "1) Add a login info card"
    print "2) Lookup a login info card"
    print "3) Remove a login info card"
    print "4) Print Login info list"
    print "9) Save and Exit"

def add_site():
    print "Add a login info card"
    site = raw_input("Site: ")
    ID = raw_input("User ID and passcard, seperated by |: ")
    sitelist[site] = ID

def lookup_site():
    print "Lookup a login info card"
    site = raw_input("Site: ")
    if sitelist.has_key(site):
        print site,sitelist[site]
    else:
        print site," was not found."

def remove_site():
     print "Remove a login info card"
     site = raw_input("Site: ")
     if sitelist.has_key(site):
         del sitelist[site]
     else:
         print site," was not found."

def print_login_info():
    print "Login Info"
    for site in sitelist.keys():
        print "Site: ",site," \tID|Passcard: ",sitelist[site]+"\n"


print "The Password Program"
print "By Nathan Pinno"
print
load_file(sitelist)
answer = getpass.getpass("What is the password? ")
while password != answer:
    print "The password is incorrect."
    answer = getpass.getpass("What is the password? ")

print "Welcome to the second half of the program."
while 1:
    main_menu()
    menu_choice = int(raw_input("Choose an option (1-4, or 9: "))
    if menu_choice == 1:
        add_site()
    elif menu_choice == 2:
        lookup_site()
    elif menu_choice == 3:
        remove_site()
    elif menu_choice == 4:
        print_login_info()
    elif menu_choice == 9:
        break
    else:
        print "That's not an option!"
save_file(sitelist)
print "Have a nice day!"

Here is the screenshot when I ran it (I've turned my password into stars for 
security) It's in the IDLE if anyone's wondering:

>>>
The Password Program
By Nathan Pinno

Warning: Problem with getpass. Passwords may be echoed.
What is the password? hello
Welcome to the second half of the program.
1) Add a login info card
2) Lookup a login info card
3) Remove a login info card
4) Print Login info list
9) Save and Exit
Choose an option (1-4, or 9: 1
Add a login info card
Site: MSN Passport
User ID and passcard, seperated by |: falcon3166 at hotmail.com|********
1) Add a login info card
2) Lookup a login info card
3) Remove a login info card
4) Print Login info list
9) Save and Exit
Choose an option (1-4, or 9: 4
Login Info
Site:  MSN Passport   ID|Passcard:  falcon3166 at hotmail.com|********

1) Add a login info card
2) Lookup a login info card
3) Remove a login info card
4) Print Login info list
9) Save and Exit
Choose an option (1-4, or 9: 9
Have a nice day!
>>> ================================ RESTART 
>>> ================================
>>>
The Password Program
By Nathan Pinno

Warning: Problem with getpass. Passwords may be echoed.
What is the password? hello
Welcome to the second half of the program.
1) Add a login info card
2) Lookup a login info card
3) Remove a login info card
4) Print Login info list
9) Save and Exit
Choose an option (1-4, or 9: 4
Login Info
Site:  MSN Passport   ID|Passcard:  MSN Passport

Site:  falcon3166 at hotmail.com|********   ID|Passcard: 
falcon3166 at hotmail.com|********

1) Add a login info card
2) Lookup a login info card
3) Remove a login info card
4) Print Login info list
9) Save and Exit
Choose an option (1-4, or 9: 9
Have a nice day!
>>>

It's almost as it's saving the data seperately, instead of together. Why 
would it be doing this, and how do I fix it?
---------------------------------------------------------------
Early to bed,
Early to rise,
Makes a man healthy, wealthy, and wise.
--Benjamin Franklin
------------------------------------------------------------------- 


More information about the Tutor mailing list