Python修炼1.学生信息管理系统(文件保存,修改后有备份恢复)


#功能主要包括对学生信息的增删改查,以及备份恢复
#编写一个学生类,可对其进行“增,删,改,查”,学生类属性包括stuID,name,sex, classID
# student.py
class Stu:
stuID = ""
name = ''
sex = 'M'
classID = 'NULL'
#属性设置get,set方法
def get_stuID(self):
return self.stuID
def set_stuID(self,stuID):
self.stuID = stuID
def get_name(self):
return self.name
def set_name(self,name):
self.name = name
def get_sex(self):
return self.sex
def set_sex(self,sex):
self.sex = sex
def get_classID(self):
return self.classID
def set_classID(self,classID):
self.classID = classID
#学生信息.py
import os
import re
import sys
import shutil
import time
import Student
global FILEPATH # 学生信息文件
global TEMPFILE # 备份文件
FILEPATH = 'student.db'
TEMPFILE = 'backup.db'
# 视图界面
def menuView():
print("--------------------------")
print("--------------------------")
print("---------欢迎使用---------")
print("-----学生信息管理系统-----")
print("--------------------------")
print("--------------------------")
print("请稍后,系统载入中....")
time.sleep(2)
main()

def main():
while True:
os.system('cls') #清屏命令
print("---------------------")
print("1.查询学生信息")
print("2.增加学生信息")
print("3.删除学生信息")
print("4.修改学生信息")
print("5.恢复学生信息")
print("0.退出")
print("---------------------")
opt = input("请选择:")
if opt == '1':
while True:
query()
opt1 = input("继续查询么(Y/N)?:")
if opt1 == 'Y' or opt1 == 'y' or opt1 == '':
continue
else:
break
elif opt == '2':
while True:
addMenu()
opt2 = input("继续增加学生么(Y/N)?:")
if opt2 == 'Y' or opt2 == 'y' or opt2 == '':
continue
else:
break
elif opt == '4':
while True:
updateMenu()
opt4 = input("继续修改么(Y/N)?:")
if opt4 == 'Y' or opt4 == 'y' or opt4 == '':
continue
else:
break
elif opt == '3':
while True:
delMenu()
opt3 = input("继续删除么(Y/N)?:")
if opt3 == 'Y' or opt3 == 'y' or opt3 == '':
continue
else:
break
elif opt == '5':
backup()
elif opt == '0':
exitProgram()
break
else:
print("Erro input!")
# 0.退出
def exitProgram():
print("谢谢使用,再见!")
# 1.查询学生信息
def query():
print("1.按学号查询\n2.按姓名查找\n3.按班级查找\n4.查询全部学生")
opt = input('请输入:')
# getInfo(int(opt))
if opt == '1':
getInfo(int(opt))
elif opt == '2':
getInfo(int(opt))
elif opt == '3':
getInfo(int(opt))
elif opt == '4':
getInfo(int(opt))
else:
print("输入错误")
return
# 获取学生信息
def getInfo(IDnum):
# 用户输入的信息
stu = ''
if IDnum == 1:
s = '学号'
IDnum -= 1
stu = input("请输入%s:" % s)
# 正则表达式对学号,班级号进行检查匹配(长度)
while True:
if re.match('^[0-9]{3}$', stu):
break
else:
print("学号为001-999")
stu = input("请输入%s:" % s)
if IDnum == 2:
s = '姓名'
IDnum -= 1
stu = input("请输入%s:" % s)
# 判断姓名输入是否为中文
while True:
if not is_Chinese(stu):
print('姓名请输入中文')
else:
break
stu = input("请输入%s:" % s)
if IDnum == 3:
s = '班级'
stu = input("请输入%s:" % s)
# 正则表达式对学号,班级号进行检查匹配(长度)
while True:
if re.match('^[0-9]{2}$', stu):
break
else:
print("班级号01-99")
stu = input("请输入%s:" % s)
# 显示所有学生信息
if IDnum == 4:
f = open(FILEPATH, 'r')
print(''.join(f.readlines()))
return
# 判断是否找到
flag = False
# 读取文件内容 找到打印出来,未找到给出提示
with open(FILEPATH) as f:
for line in f.readlines():
items = line.split('\t')
if re.match(items[IDnum], stu):
print(line, end='')
flag = True
if not flag:
print("没有该% s = %s 的学生信息" % (s, stu))
# 2.增加学生信息
def addMenu():
stu = Student.Stu() #创建对象
while True:#学号 无默认值default
stuID = input("输入学号ID(001-999):")
p = re.match('^[0-9]{3}$', stuID)#正则表达式 ^匹配开始 $匹配结束 [0-9]{3} 0-9数字匹配三次
if p:
if stuID == '000':
print("学号必须是 001-999")
continue
if isIDExist(stuID, 0):# 0代表的是学号
print("学号 = %s 已存在" % stuID)
continue
else:
stu.set_stuID(stuID)
break
else:
print("学号必须是 001-999")
while True:#姓名 无默认值
name = input("输入学生姓名:")
#print(type(name)) str类型
#p = re.match(r'^[\u4E00-\u9FFF]+$', 'name') # 正则表达式 ^匹配开始 $匹配结束 匹配多次中文
p = is_Chinese(name)
if p:
stu.set_name(name)
break
else:
print("名字必须为中文")
while True:#性别 默认值default
sex = input("输入学生性别:(M 男,W 女,默认为M)")
if sex == 'M' or sex == 'm' or sex == '': #包含默认值
stu.set_sex('M')
break
elif sex == 'W' or sex == 'w':
stu.set_sex(sex.upper())
break
else:
print('性别(M/W)')
continue
while True:#班级 默认值
classID = input("输入学生班级号(01-99,默认为NULL)")
if classID == '':
stu.set_classID('NULL')
break
p = re.match('^[0-9]{2}$', classID)
if p:
if classID == '00':
print("班级号必须为01-99")
continue
stu.set_classID(classID)
break
else:
print("班级号必须为01-99")
file1 = open(FILEPATH, 'a')#打开文件为追加模式
print('ID\t\tNAME\t\tSEX\t\tCLASS')
print(stu.get_stuID()+'\t\t'+stu.get_name()+'\t\t'+stu.get_sex()+'\t\t'+stu.get_classID())
file1.write(stu.get_stuID()+'\t'+stu.get_name()+'\t'+stu.get_sex()+'\t'+stu.get_classID()+'\t'+'\n')
print("增加学生成功")
file1.close()
# 3.删除学生信息
def delMenu():
print('1.按学号删除\n2.按班级号删除')
opt = input("请选择")
# 备份区
with open(FILEPATH, 'r') as f:
lines = f.readlines()
with open('TEMP.db', 'w') as temp:
temp.writelines(lines)
# 删除功能
if opt == '1':
delInfoID()
elif opt == '2':
delInfoClassID()
else:
print("输入错误")
# 按学号删除
def delInfoID():
stuID = input("学号:")
stuIDnum = 0
if not isIDExist(stuID, stuIDnum):
print("学号:%s 不存在" % stuID)
return
# 删除stuID学生信息
with open(FILEPATH, 'r') as f:
lines = f.readlines()
with open(FILEPATH, 'w') as f_w:
for line in lines:
items = line.split('\t')
if re.match(items[stuIDnum], stuID):
print('学号= %s的学生信息为:\n' % stuID, line)
opt = input("是否确认删除学号 = %s 的学生信息(Y/N)?" % stuID)
if opt == 'Y' or opt == 'y' or opt == '':
print("删除成功!")
continue
f_w.write(line)
# 按班级号删除
def delInfoClassID():
classID = input("班级号:")
classList = []
classNum = 3
if not isIDExist(classID, classNum): # 3代表班级号
print("班级号: %s 不存在" % classID)
return
with open(FILEPATH, 'r') as f:
lines = f.readlines()
with open(FILEPATH, 'w') as f_w:
# 打印要删除的学生信息
for line in lines:
items = line.split('\t')
if not items[classNum].count(classID) == 0:
classList.append(line)
print('班级号= %s的学生信息为:\n' % classID, '\n'.join(classList))
opt = input("是否确认删除班级号 = %s 的学生信息(Y/N)?" % classID)
if opt == 'Y' or opt == 'y':
for line in lines:
items = line.split('\t')
if re.match(items[classNum], classID):
continue
f_w.write(line)
print("删除成功!")
elif opt == 'N' or opt == 'n' or opt == '':
f_w.write(''.join(lines))
else:
print("输入错误,删除失败")
f_w.write(''.join(lines))
# 4.修改
def updateMenu():
# 备份区
with open(FILEPATH, 'r') as f:
lines = f.readlines()
with open('TEMP.db', 'w') as temp:
temp.write(''.join(lines))
# 学号唯一
stuID = input("学号:")
stuIDnum = 0
if not isIDExist(stuID, stuIDnum):
print("学号:%s 不存在" % stuID)
return
with open(FILEPATH, 'r') as f:
lines = f.readlines()
with open(FILEPATH, 'w') as f_w:
for line in lines:
items = line.split('\t')
if re.match(items[stuIDnum], stuID):
print('学号= %s的学生信息为:\n' % stuID, line)
continue
f_w.write(line)
# 重新添加学生信息(更新)
stu = Student.Stu() # 创建对象
while True:
stuID = input("输入新的学号ID(001-999):")
p = re.match('^[0-9]{3}$', stuID)
if p:
if stuID == '000':
print("学号必须是 001-999")
continue
if isIDExist(stuID, 0):
print("学号 = %s 已存在" % stuID)
continue
else:
stu.set_stuID(stuID)
break
else:
print("学号必须是 001-999")
while True:
name = input("输入新的学生姓名:")
p = is_Chinese(name)
if p:
stu.set_name(name)
break
else:
print("名字必须为中文")
while True: # 性别 默认值default
sex = input("输入新的学生性别:(M 男,W 女,默认为M)")
if sex == 'M' or sex == 'm' or sex == '':
stu.set_sex('M')
break
elif sex == 'W' or sex == 'w':
stu.set_sex(sex.upper())
break
else:
print('性别(M/W)')
continue
while True: # 班级 默认值
classID = input("输入新的学生班级号(01-99,默认为NULL)")
if classID == '':
stu.set_classID('NULL')
break
p = re.match('^[0-9]{2}$', classID)
if p:
if classID == '00':
print("班级号必须为01-99")
continue
stu.set_classID(classID)
break
else:
print("班级号必须为01-99")
file1 = open(FILEPATH, 'a') # 打开文件为追加模式
print('ID\t\tNAME\t\tSEX\t\tCLASS')
print(stu.get_stuID() + '\t\t' + stu.get_name() + '\t\t' + stu.get_sex() + '\t\t' + stu.get_classID())
file1.write(stu.get_stuID() + '\t' + stu.get_name() + '\t' + stu.get_sex() + '\t' + stu.get_classID() + '\t' + '\n')
print("修改成功")
file1.close()
# 5.恢复
def backup():
print("1.恢复上一次的学生信息\n2.恢复到系统启动时学生信息")
opt = input("请输入:")
if opt == '1':
# 在删除之后增加的数据也一同恢复
t = open('TEMP.db', 'r+')
lines = t.readlines()
with open(FILEPATH) as f:
for eachLine in f:
if eachLine in lines:
continue
t.write(eachLine)
t.close()
os.remove(FILEPATH)
os.renames('TEMP.db', FILEPATH)
print("恢复成功!")
if opt == '2':
# 将增加的数据也一同恢复
t = open(TEMPFILE, 'r+')
lines = t.readlines()
with open(FILEPATH) as f:
for eachLine in f:
if eachLine in lines:
continue
t.write(eachLine)
t.close()
os.remove(FILEPATH)
os.renames(TEMPFILE, FILEPATH)
shutil.copyfile(FILEPATH, TEMPFILE)
print("成功恢复至启动时状态")
# 公共方法
# ***判断用户输入的是否为中文
def is_Chinese(word):
for ch in word:
if '\u4e00' <= ch <= '\u9fff':
return True
return False
# ***判断学号是否已存在
# ***IDnum代表学号0,姓名1,性别2,班级3等信息
def isIDExist(stuID,IDnum):
flag = False
with open(FILEPATH) as f:
for line in f.readlines():#for line in f:
if stuID in line.strip():
flag = True
return flag '''
b = int(IDnum)
flag = False
with open(FILEPATH) as f:
for line in f:
items = line.split('\t')
if re.match(items[b], stuID):
flag = True
return flag
# 备份系统启动时的学生信息
def init():
stuBackup = open(TEMPFILE, 'w')
fp = open(FILEPATH, 'r')
stuText = fp.readlines()
stuBackup.write(''.join(stuText))
fp.close()
stuBackup.close()


if __name__ == '__main__':
init()
menuView()


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM