|
| 1 | +import json |
| 2 | + |
| 3 | +# Initialize an empty dictionary to store passwords |
| 4 | +passwords = {} |
| 5 | + |
| 6 | +# Function to add a new password |
| 7 | +def add_password(account, username, password): |
| 8 | +if account in passwords: |
| 9 | +print(f"An entry for {account} already exists.") |
| 10 | +else: |
| 11 | +passwords[account] = {'username': username, 'password': password} |
| 12 | +save_passwords() |
| 13 | +print(f"Password for {account} added successfully.") |
| 14 | + |
| 15 | +# Function to retrieve a password |
| 16 | +def get_password(account): |
| 17 | +if account in passwords: |
| 18 | +return passwords[account]['password'] |
| 19 | +else: |
| 20 | +return None |
| 21 | + |
| 22 | +# Function to list all stored accounts |
| 23 | +def list_accounts(): |
| 24 | +if passwords: |
| 25 | +print("Stored Accounts:") |
| 26 | +for account in passwords.keys(): |
| 27 | +print(account) |
| 28 | +else: |
| 29 | +print("No accounts stored.") |
| 30 | + |
| 31 | +# Function to save passwords to a file |
| 32 | +def save_passwords(): |
| 33 | +with open('passwords.json', 'w') as file: |
| 34 | +json.dump(passwords, file) |
| 35 | + |
| 36 | +# Function to load passwords from a file |
| 37 | +def load_passwords(): |
| 38 | +global passwords |
| 39 | +try: |
| 40 | +with open('passwords.json', 'r') as file: |
| 41 | +passwords = json.load(file) |
| 42 | +except FileNotFoundError: |
| 43 | +passwords = {} |
| 44 | + |
| 45 | +# Load existing passwords on startup |
| 46 | +load_passwords() |
| 47 | + |
| 48 | +# Main loop for command-line interface |
| 49 | +while True: |
| 50 | +print("\nMenu:") |
| 51 | +print("1. Add Password") |
| 52 | +print("2. Retrieve Password") |
| 53 | +print("3. List Accounts") |
| 54 | +print("4. Quit") |
| 55 | + |
| 56 | +choice = input("Enter your choice: ") |
| 57 | + |
| 58 | +if choice == '1': |
| 59 | +account = input("Enter the account name: ") |
| 60 | +username = input("Enter the username: ") |
| 61 | +password = input("Enter the password: ") |
| 62 | +add_password(account, username, password) |
| 63 | +elif choice == '2': |
| 64 | +account = input("Enter the account name: ") |
| 65 | +password = get_password(account) |
| 66 | +if password: |
| 67 | +print(f"The password for {account} is: {password}") |
| 68 | +else: |
| 69 | +print(f"No entry found for {account}.") |
| 70 | +elif choice == '3': |
| 71 | +list_accounts() |
| 72 | +elif choice == '4': |
| 73 | +break |
| 74 | +else: |
| 75 | +print("Invalid choice. Please try again.") |
| 76 | + |
| 77 | +# Save passwords before exiting |
| 78 | +save_passwords() |
0 commit comments