import os import time # 添加員工信息 def zengjia(): print('請輸入員工的基本信息:') while 1: while 1: id = input("請輸入編號").strip() if not id.isdigit(): print("... ...
import os import 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