最近在写一个项目——图书管理系统
在完成这个项目的道路上我写了2个基础的图书管理系统。
①用IO完成数据存储的图书管理系统
②用MySQL完成数据存储的图书管理系统
今天先来更新第一个用IO操作的图书管理系统!!!
话不多说,上代码!
import os
# 新增图书信息
def Add():
id_book = int(input('请输入编号:'))
name_book = input('请输入书名:')
author_book = input('请输入作者:')
type_book = input('请输入种类:')
num_book = int(input('请输入数量:'))
surplus_book = int(input('请输入剩余:'))
book_information = {'编号': id_book, '书名': name_book, '作者': author_book, '种类': type_book, '数量': num_book,
'剩余': surplus_book}
with open('图书信息.txt', 'a', encoding='utf-8') as f: # 图书信息以字典形式存储,增加信息就用a追加到文件中
if os.path.getsize('图书信息.txt') != 0:
f.write('\n') # 换行\n保证数据在文件中按行存储,这样在查询时用f.readlines()才能正确查询
f.write(str(book_information))
# 删除图书信息
def Delete():
del_num = int(input('请输入你要删除图书的编号:'))
with open('图书信息.txt', 'r', encoding='utf-8') as f: # 把文件信息全部提取出来
book_inf = f.readlines()
for i in range(len(book_inf)): # 循环查找想要编号的那一项
if eval(book_inf[i])['编号'] == del_num:
book_inf.pop(i) # 找到后把那一项删掉
break
with open('图书信息.txt', 'w', encoding='utf-8') as f: # 把删掉一项信息后的book_inf覆盖存储到文件中
for temp in book_inf:
f.write(str(temp)) # 存储必须以str类型,否则就报错
# 修改图书信息
def Alter():
alt_num = int(input('请输入你要修改图书的编号:'))
with open('图书信息.txt', 'r', encoding='utf-8') as f: # 取所有图书信息
books_inf = f.readlines()
for temp in books_inf: # 循环遍历出哪一项是想要修改的
b = eval(temp)
if b['编号'] == alt_num:
print(b) # 列出所选编号的图书信息,好看着修改
books_inf.remove(temp)
b['编号'] = int(input('请输入编号:'))
b['书名'] = input('请输入书名:')
b['作者'] = input('请输入作者:')
b['种类'] = input('请输入种类:')
b['数量'] = int(input('请输入数量:'))
b['剩余'] = int(input('请输入剩余:'))
break
with open('图书信息.txt', 'w', encoding='utf-8') as f:
f.write(str(b) + '\n')
with open('图书信息.txt', 'a', encoding='utf-8') as f:
for temp in books_inf:
f.write(str(temp))
# 查询图书信息
def Search():
sea_num = int(input('请输入你要查找图书的编号:'))
with open('图书信息.txt', 'r', encoding='utf-8') as f: # 取所有图书信息
books_inf = f.readlines()
for temp in books_inf: # 循环遍历出哪一项是想要查找的
b = eval(temp)
if b['编号'] == sea_num:
print('编号'.center(5, ' '), '书名'.center(15, ' '), '作者'.center(15, ' '), '种类'.center(15, ' '),
'数量'.center(5, ' '),
'剩余'.center(5, ' '))
format_data = '{:^5}\t{:^15}\t{:^10}\t{:^10}\t{:^5}\t{:^5}'
print(format_data.format(b.get('编号'), b.get('书名'), b.get('作者'), b.get('种类'), b.get('数量'), b.get('剩余')))
# 展示所有图书信息
def Show():
with open('图书信息.txt', 'r', encoding='utf-8') as f: # 取所有图书信息
books_inf = f.readlines()
print('编号'.center(5, ' '), '书名'.center(15, ' '), '作者'.center(15, ' '), '种类'.center(15, ' '), '数量'.center(5, ' '),
'剩余'.center(5, ' '))
format_data = '{:^5}\t{:^15}\t{:^10}\t{:^10}\t{:^5}\t{:^5}'
for temp in books_inf:
b = eval(temp)
print(format_data.format(b.get('编号'), b.get('书名'), b.get('作者'), b.get('种类'), b.get('数量'), b.get('剩余')))
# 选择菜单
def Menu():
print('-----------------------------功能菜单-----------------------------')
print('\t\t\t\t1.新增图书信息')
print('\t\t\t\t2.删除图书信息')
print('\t\t\t\t3.修改图书信息')
print('\t\t\t\t4.查询图书信息')
print('\t\t\t\t5.展示所有图书信息')
print('\t\t\t\t0.退出')
print('----------------------------------------------------------------')
# 主程序
def main():
print('=========================资料室图书管理系统=========================')
while True:
Menu()
select = eval(input('请选择功能:'))
if select == 1:
Add()
print('新增信息完成!')
print('\n')
elif select == 2:
Delete()
print('删除信息完成!')
print('\n')
elif select == 3:
Alter()
print('修改信息完成!')
print('\n')
elif select == 4:
Search()
print('\n')
elif select == 5:
Show()
print('\n')
elif select == 0:
print('谢谢使用!')
break
else:
print("输入错误!请重新输入!")
main()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
如果Python基础打得牢,写这些不成问题。如果你对以上代码有疑问,可以在评论区问我,我一定会给你解答!
效果图:
这些就是函数的基本操作,是不是很简单呀!
这些就是在文件里存储的图书数据👆
这个代码可以直接搬运,直接运行!!!