import osimport time# 添加员工信息def zengjia(): print('请输入员工的基本信息:') while 1: while 1: id = input("请输入编号").strip() if not id.isdigit(): print("格式不对,重新输入") continue with open('emp.db', mode='r', encoding='utf-8') as f: lst = [] for line in f: lst.append(line.split('_')[0]) # 将所有的编号取出来存放lst列表中 for el in lst: # 遍历列表,查看输入编号是否重复 if id == el: print('该编号已存在,请重新输入') break else: name = input("请输入名字").strip() birthday = input("请输入出生日期").strip() salary = input("请输入工资").strip() input_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()) with open('emp.db', mode='a+', encoding='utf-8') as f: f.write(id + '_' + name + '_' + birthday + '_' + salary + '_' + input_time + '\n') print('增加成功') with open('emp.log', mode='a', encoding='utf-8') as f1: d = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()) f1.write('管理员在%s增加了添加员工信息操作。' % d + '\n') tc = input('输入q退出,继续按任意键').strip() if tc.upper() == 'Q': break break#员工信息修改def xiugai(): with open('emp.db', mode='r', encoding='utf-8') as f, \ open('emp_副本.db', mode='w', encoding='utf-8') as f1: for line in f: print(line.strip()) f.seek(0) # 显示完所有员工信息,重新将光标放开头 s = input("请输入要修改的id:").strip() money = input('请输入工资:').strip() for line in f: if line[0] == s: line = line.replace(line.split('_')[3], money) # 根据编号,取出来该员工的工资,进行修改 # print(line.split('_')) f1.write(line) else: f1.write(line) os.remove('emp.db') os.rename('emp_副本.db','emp.db')#删除员工信息def shanchu(): with open('emp.db', mode='r', encoding='utf-8') as f, \ open('emp_副本.db', mode='w', encoding='utf-8') as f1: for line in f: print(line.strip()) f.seek(0) s = input("请输入要删除的id:") for line in f: if line[0] < s: f1.write(line) elif line[0] > s: # 将编号进行更改,比如删除编号3,后面的编号依次-1 line = line.replace(line.split('_')[0],str(int(line.split('_')[0]) - 1)) f1.write(line) os.remove('emp.db') os.rename('emp_副本.db','emp.db')# 查看信息def chakan(): with open('emp.db', mode='r', encoding='utf-8') as f: for line in f: print(line.strip())while 1: print('HR人力资源管理') print('1.查看员工信息 2.添加员工信息 3.修改员工信息 4.删除员工信息 5.退出') n = input('请输入选项:').strip() if n == '1': chakan() elif n == '2': zengjia() elif n == '3': xiugai() elif n == '4': shanchu() elif n == '5': exit() else: print("输入有误!请重新输入") continue