週末小作業
阿新 • • 發佈:2018-12-09
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