Contact
Management System
Summary:
Hello
people,
Are you
learning Python? Do you want to build a contact management system using Python?
So, you are in right place. On this page, you will learn to create a Contact
book project in Python.
This contact book project is created for beginners that’s why this project is completely
based on the Python console. Using this Python script, we can create the contacts
and store them in the file. And according to your requirements, you can view
that contacts in the format of a table. Also, you can search the contacts and if
you don’t need any contacts then you can delete them from the file.
Contact
book project in Python: complete details-
Project
Name |
Contact
book project in Python |
About |
A contact book is a contact management
system that is used to store the contacts in the structure of the file. This
project is specially made for beginners. This Python script help to create
the contacts, view the stored contacts, search the contacts, and delete the
contacts. |
Type |
Python
console-based project |
Published by |
Codes Gallery |
Developer |
Chetan
Bedakihale |
Before we
start making this program, we should know what the things needed to make
this program are.
Requirements
of the program:
- Name
of the contact person
- Phone
number of the contact person
- Date
and time
- CSV
file (to save the contacts in the form of tabular)
OK! now we
know what is needed for this program. Now we can make this project very easy.
So, let’s start.
Procedures
to make a simple contact management system:
- In
In the first step, we will create a folder called Contact Book. And in the
same folder, we will create a Python file called contact_book.py. And in
the same file, we will write this program.
- Now,
to make this program, we will import some important modules/libraries according
to the need of the program.
#importing required modules
import os
import csv
import datetime
from time import strftime
- Now,
we will make the menu page according to the picture shown above. Before we
create the menu page, we will create the title for the menu page. That title
will be used on all pages, so we will write this title in a global function
called title(). With this, we can use this title anywhere in the program.
#creating title for all pages
def title():
line_1 = "------------------------------------------"
Tq = " Contacts Management System"
line_2 = "------------------------------------------"
print(line_1.center(130))
print(Tq.center(130))
print(line_2.center(130))
- Now,
we will first clean the screen of the console with the help of the OS module. After
that, we’ll call the title. Then we’ll create our menu inside a while loop that
runs continuously.
- Now,
we will give some options to create, view, search, and delete contacts. Using
those options, we can create contacts, view the contacts, search the contacts,
and delete the contacts from the database or you can say CSV file.
Now, we will take input from the
user to select these options. With these inputs given by the user, we can
select any create options.
os.system('cls')
title()
while True:
print("1. View Contacts".center(128))
print("2. Create Contacts".center(129))
print("3. Search Contacts".center(129))
print("4. Delete Contacts".center(129))
print("5. Exit".center(120))
print("_______________________".center(131))
option = int(input("\t\t\t\t\t\t\tChoose you option: ")) # getting input from the user to select above options
- Now,
we will create some functions to run all these options. Those functions create (), view(), search(), and delete(). Remember, we have to write all these
functions in a class called contact_numbers. OK. We can create, view, search,
and delete contacts using all these functions.
Here, the create() function helps create the contacts. And the view() function helps to show the
created contacts in the form of a table. Here, to find the contacts, the search()
function helps. And finally, the delete() function works to delete the contacts
from the database or you can say CSV file.
#creating class
class contact_numbers:
contact_fields = ["Name", "Mobile_No"]
contact_database = "contacts.csv"
contact_data = []
#writing create() function to create contacts
def create(self):
os.system('cls')
title()
print(" Create Contact:")
print(" ---------------")
print("")
for fields in self.contact_fields:
contact_details = input(" Enter " + fields + ":")
print("")
self.contact_data.append(contact_details)
Date = datetime.datetime.today()
d = Date.strftime("%B %d %Y")
self.contact_data.append(d)
with open(self.contact_database, 'a') as file:
write = csv.writer(file)
write.writerows([self.contact_data])
self.contact_data=[]
print("")
print("Contact is created successfully".center(129))
print("\n")
#writing view() function to see created contacts
def view(self):
os.system('cls')
title()
print("Contacts:".center(10))
print("---------".center(10))
print("")
count = 0
with open(self.contact_database, 'r') as file:
read = csv.reader(file)
for data1 in read:
if len(data1) > 0:
count = count + 1
print("Total Contacts: ", count)
print("")
with open(self.contact_database, 'r') as file:
read = csv.reader(file)
if os.path.getsize(self.contact_database)==0: print("Contact Book is empty, Please create contacts".center(129))
else:
for fields in self.contact_fields:
print('{0:<10}'.format(fields).center(10), end="\t\t")
print('{0:<10}'.format("Date"))
print('{:<10}\t\t{:<10}\t\t{:<10}'.format('----','---------','----'))
print("")
for data in read:
for item in data:
print( '{:<10}'.format(item).center(10), end="\t\t")
print("")
print("\n")
input("\t Press enter key to continue..".center(120))
os.system('cls')
def search(self):
os.system('cls')
title()
print("Search Contacts:".center(10))
print("----------------".center(10))
print("")
self.contact_person_match = 'false'
search_value = input("Enter your name: ")
print("")
for fields in self.contact_fields:
print('{0:<10}'.format(fields).center(10), end="\t\t")
print('{0:<10}'.format("Date"))
print('{:<10}\t\t{:<10}\t\t{:<10}'.format('----','---------','----').center(10))
print("")
with open(self.contact_database, 'r') as file:
read = csv.reader(file)
for data in read:
if len(data)>0:
if search_value == data[0]:
self.contact_person_match = 'true'
print( '{:<10}\t\t{:<10}\t\t{:<10}'.format(data[0], data[1], data[2]).center(10))
if self.contact_person_match == 'false':
print("")
print("Sorry!, there is no contact by this name".center(129))
print("")
#input("\t press enter key to continue..")
def delete(self):
os.system('cls')
title()
print("")
print("Delete Contacts:")
print("----------------")
print("")
self.contact_person_match = 'false'
update_contact = input("Enter the name: ")
update_list = []
with open(self.contact_database, 'r') as file:
read = csv.reader(file)
for data in read:
if len(data)>0:
if update_contact != data[0]:
update_list.append(data)
else:
self.contact_person_match = 'true'
if self.contact_person_match == 'true':
with open(self.contact_database, 'w') as file:
write = csv.writer(file)
write.writerows(update_list)
print("")
print("Contact is deleted successfully!".center(129))
print("")
if self.contact_person_match == 'false':
print("")
print("Sorry! data not found")
print("")
#creating object of the class
contact_class = contact_numbers()
10>10>
- Here,
we have created all the functions. Now, we will put some conditions on using these
functions. The first condition will be that if the user selects the first
option, then the view function will be called.
If the user selects the second option,
then the create function gets called. If the user selects the third option, the
search function is called. If the user selects the fourth option, the delete
function will be called. If the user selects the fifth option, the program will be
directly exited.
- Here, we will write the create, search,
and delete functions in a while loop so that if we have to run any process, again and again, we do not have to go to the menu page repeatedly.
if option == 1:
contact_class.view() #calling view() function
title()
if option == 2:
while True:
contact_class.create() #calling create() function
ans = input("\t\t\t\t\tDo you want to create another contact number?[Y/N]: ")
if ans == 'Y' or ans == 'y':
continue
else: break
os.system('cls')
title()
if option == 3:
while True:
contact_class.search() #calling search() function
print("")
ans = input("\t\t\t\t\tDo you want to search another contact number?[Y/N]: ")
if ans == 'Y' or ans == 'y':
continue
else: break
os.system('cls')
title()
if option == 4:
while True:
contact_class.delete() #calling delete() function
ans = input("\t\t\t\t\tDo you want to delete another contact number?[Y/N]: ")
if ans == 'Y' or ans == 'y':
continue
else: break
os.system('cls')
title()
if option == 5:
#writing exit code
os.system('cls')
line_1 = "------------------------------------------"
Tq = " Thank you for using this software"
line_2 = "------------------------------------------"
print(line_1.center(130))
print(Tq.center(130))
print(line_2.center(130))
break
if option > 5 or option < 1:
#error message for selecting the wrong option
os.system('cls')
print("Invalid choice. Please choose valid option".center(129))
print("\n")
input("Press enter key to continue...".center(130))
os.system('cls')
title()
Post a Comment
You are welcome to share your ideas with me in the comments!