I'm starting my first project (a password manager). What I have done so far is make it so the user can input whether they want to make a new password or look for a password. If they choose to enter a password, the account/purpose for the password and the actual password will be saved to a dictionary. For example, a purpose could be for "yahoo" and the password being "example". That dictionary is then written down in a text file. If the user decides to look for a password, all they would have to do is type in the account for the password. So far everything is working except for the fact that when I enter another password and account, it overwrites the pre-existing password and account instead of adding the new password to the dictionary.
import json
passwords = {
}
prompt = "If you want to make a new password, type 'Make password'."
prompt = "\nIf you want to look for a password, type 'Look for password'.\n"
answer = input(prompt)
def password_list(account_name, password_name):
passwords[account_name] = password_name
answer = answer.upper()
found = 0 # used to see whether account for password can be found
if answer == "MAKE PASSWORD":
account_name = input("What use is this password for? ")
account_name = account_name.upper()
password_name = input("What is the password? ")
password_list(account_name, password_name) # purpose and password saved to dict
with open("passwords.txt", 'w ') as f:
f.write(json.dumps(passwords))
print("Your password was saved!") # dictionary gets saved to text file
elif answer == "LOOK FOR PASSWORD":
with open("passwords.txt", "r") as f:
passwords = json.loads(f.read()) # text file gets opened to read
if not passwords: # if the list is empty...
print("Sorry but there are no passwords available. Make a new one!")
elif passwords: #if the list isn't empty...
search_account = input("What account is this password for? ")
search_account = search_account.upper()
for name in passwords.keys(): # list of accounts get searched
if search_account == name: #if an account is in the dictionary
print(f"The password is '{passwords.get(name)}'.")
found = 1
break
if found != 1:
print("Sorry, we can't find such name.")
CodePudding user response:
Cool project.
It's because every time you start the script you force the password dic to be empty. So when you add a password, it's added to a new empty dic and than you overwrite the file with this empty dic new_password.
When you code, think about the most likely outcome at every run: the file exists. IF it doesn't, than create it.
This is what I demonstrate here in the load_passwords()
function.
As an extra, I propose to you a more Pythonic (and efficient) way to search through a dictionary's keys in O(1) rather than O(n).
import json
prompt = "If you want to make a new password, type 'Make password'."
prompt = "\nIf you want to look for a password, type 'Look for password'.\n"
answer = input(prompt).upper()
def load_passwords():
try:
with open("passwords.txt", "r") as f:
passwords = json.loads(f.read()) # text file gets opened to read
return passwords
except FileNotFoundError:
print("Sorry but there are no passwords available. Make a new one!")
return {}
def password_list(account_name, password_name):
passwords = load_passwords()
passwords[account_name] = password_name
return passwords
if answer == "MAKE PASSWORD":
account_name = input("What use is this password for? ").upper()
password_name = input("What is the password? ")
passwords = password_list(account_name, password_name) # purpose and password saved to dict
with open("passwords.txt", 'w ') as f:
f.write(json.dumps(passwords))
print("Your password was saved!") # dictionary gets saved to text file
elif answer == "LOOK FOR PASSWORD":
passwords = load_passwords()
if passwords: #if the list isn't empty...
search_account = input("What account is this password for? ").upper()
# this is much better
if search_account in passwords:
print(f"The password is '{passwords.get(search_account)}'.")
else:
print("Sorry, we can't find such name.")
Note: Be sure to encrypt your passwords before saving to a file.
CodePudding user response:
"You should probably initialize your password list by reading your json file if it's there. Otherwise, when you run MAKE PASSWORD, it adds the new password to an empty dict and overwrites the existing password file which might've had a password in there before." – rchome