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.
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)
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")
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.
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.
Spotted a bug, broken code, or something that doesn't look right? Tell us what's off and we'll fix it.