python图书管理系统代码(功能齐全版)
时间: 2023-09-09 14:05:47 浏览: 115
以下是一个功能齐全的Python图书管理系统代码,包含基本的增删改查和文件存储等功能:
```python
# -*- coding: utf-8 -*-
# 图书管理系统
import os
# 图书列表
books = []
# 加载图书列表
def load_books():
global books
if os.path.exists("books.txt"):
with open("books.txt", "r", encoding="utf-8") as f:
for line in f:
name, author = line.strip().split(",")
books.append({"name": name, "author": author})
# 保存图书列表
def save_books():
with open("books.txt", "w", encoding="utf-8") as f:
for book in books:
f.write("{},{}\n".format(book["name"], book["author"]))
# 添加图书
def add_book():
name = input("请输入图书名称:")
author = input("请输入作者名称:")
books.append({"name": name, "author": author})
print("添加成功!")
# 删除图书
def remove_book():
name = input("请输入要删除的图书名称:")
for book in books:
if book["name"] == name:
books.remove(book)
print("删除成功!")
return
print("未找到该图书!")
# 修改图书
def update_book():
name = input("请输入要修改的图书名称:")
for book in books:
if book["name"] == name:
author = input("请输入新的作者名称:")
book["author"] = author
print("修改成功!")
return
print("未找到该图书!")
# 查询图书
def search_book():
name = input("请输入要查询的图书名称:")
for book in books:
if book["name"] == name:
print("图书名称:", book["name"])
print("作者名称:", book["author"])
return
print("未找到该图书!")
# 显示图书列表
def list_books():
if len(books) == 0:
print("暂无图书!")
return
print("图书列表:")
for book in books:
print("图书名称:", book["name"])
print("作者名称:", book["author"])
# 主函数
def main():
load_books()
while True:
print("欢迎使用图书管理系统!")
print("1. 添加图书")
print("2. 删除图书")
print("3. 修改图书")
print("4. 查询图书")
print("5. 显示图书列表")
print("6. 退出系统")
choice = input("请选择操作:")
if choice == "1":
add_book()
save_books()
elif choice == "2":
remove_book()
save_books()
elif choice == "3":
update_book()
save_books()
elif choice == "4":
search_book()
elif choice == "5":
list_books()
elif choice == "6":
print("谢谢使用!")
break
else:
print("无效的操作!")
if __name__ == "__main__":
main()
```
注意:此代码仅供参考,实际应用中可能需要进行更加严谨的异常处理和数据存储等操作。
阅读全文