Python File Phonebook Program

By | April 17, 2020

Topic: Python file Phonebook
Python file Phonebook Program: Write a Python Program using text files to create and maintain a Phone Book.

Python File Phonebook Program

Python File Phonebook Program

Python Phone book Program uses file to provide the following functions:

  1. You can add new contact records
  2. It will display all the contact records stored already
  3. Search function will provide you the searching option to search any of the stored contacts from the phone book.

The Phone Book will display the following Menu and will support
the corresponding functionality:

*** Phone Book Menu ***
Enter 1,2,3 or 4:
Enter 1 To Display Your Contacts Records
Enter 2 To Add a New Contact Record a new contact
Enter 3 To search your contacts
Enter 4 To Quit
**********************

Note(update: May 29, 2021): A Short and better version of Python phone book program is added at the end of this post.

 

'''
Write a Python Program using text files
to create and maintain a Phone Book.
The Phone Book will display the
following Menu and will support
the corresponding functionality

*** Phone Book Menu ***
Enter 1,2,3 or 4:
Enter 1 To Display Your Contacts Records
Enter 2 To Add a New Contact Record a new contact
Enter 3 To search your contacts
Enter 4 To Quit
**********************

author: www.Easycodebook.com (c)
'''
file_name = "d://phonebook.txt"
file1 = open(file_name, "a+")
file1.close


def show_main_menu():
    ''' Show main menu for Phone Book Program '''
    print("\n   *** Phone Book Menu ***\n"+
          "author: www.Easycodebook.com (c)\n"+
          "------------------------------------------\n"+
          "Enter 1,2,3 or 4:\n"+
          "Enter 1 To Display Your Contacts Records\n" +
          "Enter 2 To Add a New Contact Record\n"+
          "Enter 3 To search your contacts\n"+
          "Enter 4 To Quit\n**********************")
    choice = input("Enter your choice: ")
    if choice == "1":
        file1 = open(file_name, "r+")
        file_contents = file1.read()
        if len(file_contents) == 0:
            print("Phone Book is empty")
        else:
            print (file_contents)
        file1.close
        ent = input("Press Enter to continue ...")
        show_main_menu()
    elif choice == "2":
        enter_contact_record()
        ent = input("Press Enter to continue ...")
        show_main_menu()
    elif choice == "3":
        search_contact_record()
        ent = input("Press Enter to continue ...")
        show_main_menu()
    elif choice== "4":
        print("Thanks for using Phone Book Program presented by:"+
              "\nwww.EasyCodeBook.com "+"\nFor Perfect Programming Tutorials: Python, Java, C, C++")
    else:
        print("Wrong choice, Please Enter [1 to 4]\n")
        ent = input("Press Enter to continue ...")
        show_main_menu()
        
def search_contact_record():
    ''' This function is used to searches a specific contact record '''
    search_name = input("Enter First name for Searching contact record: ")
    rem_name = search_name[1:]
    first_char = search_name[0]
    search_name = first_char.upper() + rem_name
    file1 = open(file_name, "r+")
    file_contents = file1.readlines()
     
    found = False   
    for line in file_contents:
        if search_name in line:
            print("Your Required Contact Record is:", end=" ")
            print (line)
            found=True
            break
    if  found == False:
        print("There's no contact Record in Phone Book with name = " + search_name )

def input_fname():
    ''' Converts first letter of your name  to Upper case '''
    fname = input("Enter First name ")
    rem_fname = fname[1:]
    first_char = fname[0]
    return first_char.upper() + rem_fname

def input_lname():
    ''' Converts first letter of  last name  to Upper case '''
    lname = input("Enter Last name ")
    rem_lname = lname[1:]
    first_char = lname[0]
    return first_char.upper() + rem_lname


def enter_contact_record():
    ''' It  collects contact info firstname, last name, email and phone '''
   
    first = input_fname()
    last = input_lname()
    phone = input('Enter Phone number ')
    email = input('Enter E-mail ')
    contact = ("[" + first + " " + last + ", " + phone + ", " + email +  "]\n")
    file1 = open(file_name, "a")
    file1.write(contact)
    print( "This contact\n " + contact + "has been added successfully!")

show_main_menu()

Output of A Sample Run of Python Phonebook Program using File

Note: The output will be in a single color:D

*** Phone Book Menu ***
author: www.Easycodebook.com (c)
------------------------------------------
Enter 1,2,3 or 4:
Enter 1 To Display Your Contacts Records
Enter 2 To Add a New Contact Record
Enter 3 To search your contacts
Enter 4 To Quit
**********************
Enter your choice: 1
[Ahmad Naveed, 0300-776767, ahmad@gmail.com]
[Toqir Nasir, 0334-789777, asad@gmail.com]
[Nasir Ahmad, 03001234567, nasir@gmail.com]
[Javed Iqbal, 0334-75009876, javed1bal@yahoo.com]

Press Enter to continue ...

   *** Phone Book Menu ***
author: www.Easycodebook.com (c)
------------------------------------------
Enter 1,2,3 or 4:
Enter 1 To Display Your Contacts Records
Enter 2 To Add a New Contact Record
Enter 3 To search your contacts
Enter 4 To Quit
**********************
Enter your choice: 2
Enter First name zahoor
Enter Last name khan
Enter Phone number 03006743249
Enter E-mail zakhan@gmail.com
This contact
 [Zahoor Khan, 03006743249, zakhan@gmail.com]
has been added successfully!
Press Enter to continue ...

   *** Phone Book Menu ***
author: www.Easycodebook.com (c)
------------------------------------------
Enter 1,2,3 or 4:
Enter 1 To Display Your Contacts Records
Enter 2 To Add a New Contact Record
Enter 3 To search your contacts
Enter 4 To Quit
**********************
Enter your choice: 3
Enter First name for Searching contact record: zahoor
Your Required Contact Record is: [Zahoor Khan, 03006743249, zakhan@gmail.com]

Press Enter to continue ...

   *** Phone Book Menu ***
author: www.Easycodebook.com (c)
------------------------------------------
Enter 1,2,3 or 4:
Enter 1 To Display Your Contacts Records
Enter 2 To Add a New Contact Record
Enter 3 To search your contacts
Enter 4 To Quit
**********************
Enter your choice: 555
Wrong choice, Please Enter [1 to 4]

Press Enter to continue ...

   *** Phone Book Menu ***
author: www.Easycodebook.com (c)
------------------------------------------
Enter 1,2,3 or 4:
Enter 1 To Display Your Contacts Records
Enter 2 To Add a New Contact Record
Enter 3 To search your contacts
Enter 4 To Quit
**********************
Enter your choice: 4
Thanks for using Phone Book Program presented by:
www.EasyCodeBook.com 
For Perfect Programming Tutorials: Python, Java, C, C++

Update: 29-05-2021

A Shorter and Better Version

'''
Write a Python Program using text files
to create and maintain a Phone Book.
The Phone Book will display the
following Menu and will support
the corresponding functionality

*** Phone Book Menu ***
Enter 1,2,3 or 4:
Enter 1 To Display Your Contacts Records
Enter 2 To Add a New Contact Record a new contact
Enter 3 To search your contacts
Enter 4 To Quit
**********************

author: www.Easycodebook.com (c)
'''
file_name = "d://phonebook.txt"
file1 = open(file_name, "a+")
file1.close


def show_main_menu():
    ''' Show main menu for Phone Book Program '''
    print("\n   *** Phone Book Menu ***\n"+
          "author: www.Easycodebook.com (c)\n"+
          "------------------------------------------\n"+
          "Enter 1,2,3 or 4:\n"+
          "Enter 1 To Display Your Contacts Records\n" +
          "Enter 2 To Add a New Contact Record\n"+
          "Enter 3 To Search your contacts\n"+
          "Enter 4 To Quit\n**********************")
    choice = input("Enter your choice: ")
    if choice == "1":
        file1 = open(file_name, "r+")
        file_contents = file1.read()
        if len(file_contents) == 0:
            print("Phone Book is empty")
        else:
            print (file_contents)
        file1.close
        ent = input("Press Enter to continue ...")
        show_main_menu()
    elif choice == "2":
        enter_contact_record()
        ent = input("Press Enter to continue ...")
        show_main_menu()
    elif choice == "3":
        search_contact_record()
        ent = input("Press Enter to continue ...")
        show_main_menu()
    elif choice== "4":
        print("Thanks for using Phone Book Program presented by:"+
              "\nwww.EasyCodeBook.com "+"\nFor Perfect Programming Tutorials: Python, Java, C, C++")
    else:
        print("Wrong choice, Please Enter [1 to 4]\n")
        ent = input("Press Enter to continue ...")
        show_main_menu()
        
def search_contact_record():
    ''' This function is used to searches a specific contact record '''
    search_name = input("Enter First name for Searching contact record: ")

    search_name = search_name.title()
    file1 = open(file_name, "r+")
    file_contents = file1.readlines()
     
    found = False   
    for line in file_contents:
        if search_name in line:
            print("Your Required Contact Record is:", end=" ")
            print (line)
            found=True
            break
    if  found == False:
        print("There's no contact Record in Phone Book with name = " + search_name )

def enter_contact_record():
    ''' It  collects contact info firstname, last name, email and phone '''
   
    first = input('Enter First Name: ')
    first = first.title()
    last = input('Enter Last Name: ')
    last = last.title()
    phone = input('Enter Phone number: ')
    email = input('Enter E-mail: ')
    contact = ("[" + first + " " + last + ", " + phone + ", " + email +  "]\n")
    file1 = open(file_name, "a")
    file1.write(contact)
    print( "This contact\n " + contact + "has been added successfully!")

show_main_menu()

You will also like:

and more programs on Files

and some more File programs in C Language

 18,736 total views,  65 views today

One thought on “Python File Phonebook Program

Leave a Reply

Your email address will not be published. Required fields are marked *