写一个通讯录管理系统,要求文件输入输出
时间: 2024-06-14 17:03:58 浏览: 25
以下是一个简单的通讯录管理系统的示例,实现了文件的输入和输出功能:
```python
class Contact:
def __init__(self, name, address, phone, qq, email):
self.name = name
self.address = address
self.phone = phone
self.qq = qq
self.email = email
def add_contact(contacts):
name = input("请输入联系人姓名:")
address = input("请输入联系人地址:")
phone = input("请输入联系人手机号:")
qq = input("请输入联系人QQ号:")
email = input("请输入联系人邮箱:")
contact = Contact(name, address, phone, qq, email)
contacts.append(contact)
print("联系人添加成功!")
def delete_contact(contacts):
name = input("请输入要删除的联系人姓名:")
for contact in contacts:
if contact.name == name:
contacts.remove(contact)
print("联系人删除成功!")
return
print("未找到该联系人!")
def search_contact(contacts):
name = input("请输入要查找的联系人姓名:")
for contact in contacts:
if contact.name == name:
print("联系人信息如下:")
print("姓名:", contact.name)
print("地址:", contact.address)
print("手机号:", contact.phone)
print("QQ号:", contact.qq)
print("邮箱:", contact.email)
return
print("未找到该联系人!")
def save_contacts(contacts):
file_name = input("请输入要保存的文件名:")
with open(file_name, "w") as file:
for contact in contacts:
file.write(contact.name + "," + contact.address + "," + contact.phone + "," + contact.qq + "," + contact.email + "\n")
print("通讯录保存成功!")
def load_contacts(contacts):
file_name = input("请输入要加载的文件名:")
with open(file_name, "r") as file:
lines = file.readlines()
for line in lines:
data = line.strip().split(",")
contact = Contact(data[0], data[1], data[2], data[3], data[4])
contacts.append(contact)
print("通讯录加载成功!")
def show_menu():
print("欢迎使用通讯录管理系统!")
print("1. 添加联系人")
print("2. 删除联系人")
print("3. 查找联系人")
print("4. 保存通讯录到文件")
print("5. 从文件加载通讯录")
print("0. 退出系统")
def main():
contacts = []
while True:
show_menu()
choice = input("请输入您的选择:")
if choice == "1":
add_contact(contacts)
elif choice == "2":
delete_contact(contacts)
elif choice == "3":
search_contact(contacts)
elif choice == "4":
save_contacts(contacts)
elif choice == "5":
load_contacts(contacts)
elif choice == "0":
break
else:
print("无效的选择,请重新输入!")
if __name__ == "__main__":
main()
```
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)