Freedom Sale
Independence Day Special — Unlock the AI Path 70% off our most popular AI course · Limited time offer
--Days
--Hrs
--Min
--Sec
Claim Your Discount
✦ Beginner ⏱ 25 min

📒 Build a Contact Book CLI App with Python

🎯 What You'll Build

A command-line contact book that stores names, phones, emails, and notes in a JSON file — with add, search, edit, delete, and export to CSV.

📋 What You'll Need

1

Load and save contacts from JSON

All contacts live in a single JSON file. Two helper functions handle all reading and writing.

import json, os, csv

DB = 'contacts.json'

def load():
    return json.load(open(DB)) if os.path.exists(DB) else {}

def save(contacts):
    with open(DB, 'w') as f:
        json.dump(contacts, f, indent=2)
2

CRUD operations

Add, search, edit, and delete contacts.

def add_contact(contacts):
    name = input("Name: ").strip()
    if not name: return
    contacts[name.lower()] = {
        'name':  name,
        'phone': input("Phone: ").strip(),
        'email': input("Email: ").strip(),
        'notes': input("Notes: ").strip(),
    }
    save(contacts)
    print(f"✅ {name} added.")

def search(contacts):
    q = input("Search name: ").lower()
    results = {k: v for k, v in contacts.items() if q in k}
    if not results:
        print("No contacts found.")
    for c in results.values():
        print(f"\n👤 {c['name']}  📞 {c['phone']}  ✉️ {c['email']}")
        if c['notes']: print(f"   Notes: {c['notes']}")

def delete_contact(contacts):
    name = input("Delete name: ").lower()
    if name in contacts:
        del contacts[name]; save(contacts)
        print("Deleted.")
    else:
        print("Not found.")

def export_csv(contacts):
    with open('contacts.csv', 'w', newline='') as f:
        writer = csv.DictWriter(f, fieldnames=['name','phone','email','notes'])
        writer.writeheader()
        writer.writerows(contacts.values())
    print(f"Exported {len(contacts)} contacts to contacts.csv")
3

Main menu

Wire everything into a simple menu loop.

def main():
    contacts = load()
    menu = {'1': add_contact, '2': search, '3': delete_contact, '4': export_csv}
    while True:
        print("\n📒 Contact Book")
        print("1) Add  2) Search  3) Delete  4) Export CSV  5) Quit")
        choice = input("Choice: ").strip()
        if choice == '5': break
        if choice in menu: menu[choice](contacts)

main()
📒 Contact Book
1) Add  2) Search  3) Delete  4) Export CSV  5) Quit
Choice: 1
Name: Priya Sharma
Phone: +91 98765 43210
Email: priya@example.com
Notes: Met at PyCon India 2026
✅ Priya Sharma added.
💡 Tip: Extend this with fuzzy search using difflib.get_close_matches() — it handles typos in names automatically without any extra libraries.

🎉 You Did It!

You built a fully persistent CLI app in pure Python stdlib. The same JSON file approach works for any lightweight data store — settings, history, cache.

Found something wrong?

Spotted a bug, broken code, or something that doesn't look right? Tell us what's off and we'll fix it.