Password Manager Project (code) in Python

← Back to Projects

Password Manager in Python.

About the project: This is a Password Manager project in Python. This is a self-contained script that allows you to securely store and retrieve your passwords from your console.

This program will be a simple, console-based application that stores password entries (a service name and the corresponding password) in a local file. The data will be saved to a JSON file, so your entries are persistent.

    Important Security Note
  • This is a basic, educational project.
  • The passwords are saved in a simple, non-encrypted format in a local file. Do not use this application to store your real passwords or other sensitive information.
  • A real-world password manager would require robust encryption and a master password for security.

As noted above, this project is designed for learning and demonstration.

If you wanted to make a real, secure password manager, the next steps would be to add a master password and implement a strong encryption algorithm (like AES) to protect the data in the passwords.json file.


Project Level: Intermediate

You can directly copy the below snippet code with the help of green copy button, paste it and run it in any Python editor you have.

Steps: Follow these steps

Step 1: Copy below code using green 'copy' button.

Step 2: Paste the code on your chosen editor.

Step 3: Save the code with filename and .py extention.

Step 4: Run (Press F5 if using python IDLE)




# password_manager.py

import json
import os
import getpass # A library to securely read passwords without echoing to the console.

# Filename for storing passwords
PASSWORDS_FILE = "passwords.json"

def load_passwords():
    """
    Loads password entries from a JSON file. Returns a dictionary of passwords.
    If the file doesn't exist, it returns an empty dictionary.
    """
    if os.path.exists(PASSWORDS_FILE) and os.stat(PASSWORDS_FILE).st_size > 0:
        try:
            with open(PASSWORDS_FILE, 'r') as f:
                return json.load(f)
        except json.JSONDecodeError:
            print("Warning: Passwords file is corrupted or empty. Starting with a new password book.")
            return {}
    return {}

def save_passwords(passwords):
    """
    Saves the passwords dictionary to a JSON file.
    """
    with open(PASSWORDS_FILE, 'w') as f:
        json.dump(passwords, f, indent=4)
    print("Passwords saved successfully.")

def add_password(passwords):
    """
    Prompts the user for a new password entry and adds it to the dictionary.
    """
    service = input("Enter the service name (e.g., Google, Twitter): ").strip()
    if not service:
        print("Service name cannot be empty. Password not added.")
        return
    
    # Use getpass to hide the password input in the console
    password = getpass.getpass("Enter the password: ").strip()

    if not password:
        print("Password cannot be empty. Password not added.")
        return

    passwords[service.lower()] = password
    print(f"Password for '{service}' added.")

def get_password(passwords):
    """
    Retrieves and displays a password for a given service name.
    """
    service = input("Enter the service name to retrieve the password for: ").strip()
    if not service:
        print("Service name cannot be empty.")
        return

    retrieved_password = passwords.get(service.lower())
    if retrieved_password:
        print(f"\nPassword for '{service}': {retrieved_password}")
    else:
        print(f"No password found for service '{service}'.")

def main():
    """
    Main function to run the Password Manager app with a menu.
    """
    passwords = load_passwords()

    print("--- Python Simple Password Manager ---")
    
    while True:
        print("\nMenu:")
        print("1. Add a new password")
        print("2. Get a password")
        print("3. Quit and Save")

        choice = input("Enter your choice (1-3): ").strip()

        if choice == '1':
            add_password(passwords)
        elif choice == '2':
            get_password(passwords)
        elif choice == '3':
            save_passwords(passwords)
            print("Exiting Password Manager. Goodbye!")
            break
        else:
            print("Invalid choice. Please enter a number between 1 and 3.")

# This ensures that main() is called only when the script is executed directly.
if __name__ == "__main__":
    main()





← Back to Projects