In: Computer Science
Contact Manager
COMMAND MENU
----------------------------------------------------------
list - Display all contacts
view - View a contact
add - Add a contact
del - Delete a contact
exit - Exit program
Command: list
1. Tom van Rossum
2. Edward Idle
Command: view
Number: 2
Name: Edward Idle
Email: [email protected]
Phone: +44 20 7946 0958
Command: add
Name: John Smith
Email: [email protected]
Phone: 559-123-4567
John Smith was added.
Command: list
1. Tom van Rossum
2. Edward Idle
3. John Smith
Command: exit
Bye!
Specifications
Use the attached CSV file named contacts.csv.
When the program starts, it should read the contacts from the CSV file.
For the view and del commands, display an error message if the user enters an invalid
contact number.
When you add or delete a contact, the change should be saved to the CSV file
immediately. That way, no changes are lost, even if the program crashes later.
import csv,os;
## add new entry to the contact file
def add(name, email, phone):
with open('contacts.csv','a', newline='') as csv_file:
writter = csv.writer(csv_file, delimiter=',');
writter.writerow([name,email,phone]);
csv_file.close();
print(name,"was added");
##list all contacts present in the csv file
def list():
contacts = [];
with open('contacts.csv','r') as csv_file:
reader = csv.reader(csv_file);
for line in reader:
contacts.append(line[0]);
csv_file.close();
count = 1;
for cont in contacts:
print(count,".",cont);
count +=1;
## delete contact from the list based on the phone number
def delete(phone):
with open('contacts.csv','r') as input, open('contacts1.csv','a', newline='') as output:
writer = csv.writer(output);
contacts = [];
for line in csv.reader(input):
contacts.append(line);
for cont in contacts:
if cont[2] != phone:
writer.writerow(cont);
os.remove('contacts.csv');
os.rename('contacts1.csv','contacts.csv');
print("contact is deleted", phone);
## view contact details of the user.
def view(phone):
data =[];
count =0;
with open('contacts.csv','r') as csv_file:
reader = csv.reader(csv_file);
for line in reader:
count+=1;
if line[2] == phone:
data = line;
csv_file.close();
print("Number:", count);
print("Name:", data[0]);
print("Email:", data[1]);
print("Phone:", data[2]);
##Contact Manager simulator.
print("Contact Manager");
flag = 0;
while flag != 1:
print("COMMAND MENU");
print("list - Display all contacts");
print("view - View a contact");
print("add - Add a contact");
print("del - Delete a contact");
print("exit - Exit program");
choice = input("Enter your choice:");
if choice == 'list':
list();
else:
if choice == 'add':
name = input("Enter name:");
email = input("Enter email: ");
phone = input("Enter phone number: ");
add(name,email,phone);
else:
if choice == 'view':
phone = input("Enter phone number:");
view(phone);
else:
if choice == 'del':
phone = input("Enter phone number:");
delete(phone);
else:
if choice == 'exit':
flag = 1;
I tested and it run according to the requirements
Please please please upvote it will help me alot
Please dont downvote