python实现HR人力管理系统,读写文件更新数据

HR人力资源管理系统

  • 菜单:
  •   ("查找员工信息","添加员工信息", "修改员⼯信息", "删除员⼯信息", "打印员工信息","退出")
    
  • 添加员⼯工信息:
  •   ⽤户输入员工的基本信息(id, name, birthday, salary, input_time),
      将员⼯信息写入到⽂文件employees.db⽂文件内
    
  • 修改员⼯信息:
  •   显示所有员工信息. 然后让⽤户选择要修改的员⼯工的id. 然后让⽤户输⼊员工的工资,
      将员工的工资修改为⽤户输入的⼯工资. 其余内容不做改动
    
  • 删除员⼯信息:
  •   显示所有员工信息. 然后用户选择要删除的员工id, 根据用户输入的id删除该员工的全部信息。
    
  • 查找员工信息:
  •   打印出指定员工的基本信息.
    
  • 查看员工信息:
  •   打印出所有员工的基本信息.
    

文件读写管理员工信息增删改查,采用注册ID确保唯一性。

import os

menu = \
"""
********************************** Welcome to hr managermanet system ****************************************
                                       
                                        1、查找员工信息
                                        2、添加员工信息
                                        3、修改员工信息
                                        4、删除员工信息
                                        5、打印员工信息
                                        6、退出

"""
def show_info():
    f = open('employees.db',mode='r',encoding='utf-8')
    print("{0:^2}\t{1:^4}\t{2:^4}\t{3:^4}\t{4:^4}".format('id','name','birthday','salary','input_time',chr(12288)))
    # print("id\tdname\tbirthday\tsalary\tinput_time")

    for line in f:
        line_lst = line.strip().split('|')
        # print(line_lst[0], line_lst[1], line_lst[2], line_lst[3], line_lst[4])
        print('\t'.join(line_lst))
    f.close()

def check_info():

    user_id = input("请输入查找员工ID:")
    f = open('employees.db',mode='r',encoding='utf-8')
    for line in f:
        line_lst = line.strip().split("|")
        if user_id == line_lst[0]:
            print("{0:^2}\t{1:^4}\t{2:^4}\t{3:^4}\t{4:^4}".format('id', 'name', 'birthday', 'salary', 'input_time',chr(12288)))
            print('\t'.join(line_lst))
            break
    else:   # 执行到这里,说明未执行 break
        print("该员工信息不存在!")
        return

def get_id():

    """
    获取db文件中最后一个员工id
    :return:
    """
    
    if os.path.getsize('employees.db') != 0:                       # 判断文件内容不为空

        with open('employees.db',mode='r',encoding='utf-8') as f:

            lines = f.readlines()
            last_line = lines[-1]
            last_id = int(last_line.strip().split("|")[0])

    else:

        last_id = 0

    return "{:0>3d}".format(last_id)                               # 返回 三位数的id整型

def add_employees():

    id = get_id()

    name = input("请输入员工姓名:")
    birthday = input("请输入员工生日:")
    salary = input("请输入员工的工资:")
    input_time = input("请输入录入时间:")

    f = open('employees.db',mode='a',encoding='utf-8')
    id = int(id) + 1
    f.write(str(id) +'|'+name+'|'+birthday+'|'+salary+'|'+input_time+'\n')
    f.flush()
    f.close()
    
    print("{0}员工添加成功!".format(id))

def upd_employees():

    user_id = input("请输入修改员工的ID:")
    f = open('employees.db',mode='r',encoding='utf-8')
    
    for line in f:
        lst = line.strip().split("|")
        if user_id == lst[0]:
            break
    else:
        print('未找到此员工信息!')
        return  # 停止程序
        
    f.close()

    changed_money = input("请输入修改工资大小为:")

    with open('employees.db',mode='r',encoding='utf-8') as f1,\
         open('employees.db_copy',mode='a',encoding='utf-8') as f2:

        for line in f1:
            line_lst = line.strip().split('|')
            if line_lst[0] == user_id:
                line_lst[3] = changed_money
                f2.write("|".join(line_lst)+'\n')
            else:
                f2.write(line)

    os.remove('employees.db')
    os.rename('employees.db_copy','employees.db')

    print("完成修改{0}员工信息!".format(user_id))

def del_employees():  # 删除文件内的某些内容实际上就是对文件进行修改

    user_id = input("请输入修改员工的ID:")
    f = open('employees.db',mode='r',encoding='utf-8')
    for line in f:
        lst = line.strip().split("|")
        if user_id == lst[0]:
            break
    else:
        print('未找到此员工信息!')
        return  # 停止程序

    f.close()

    with open('employees.db',mode='r',encoding='utf-8') as f1,\
         open('employees.db_copy',mode='a',encoding='utf-8') as f2:      # with 打开的文件不用管 close

         for line in f1:
             line_lst = line.strip().split('|')
             if line_lst[0] == user_id:
                 continue   # 找到该行,不处理
             f2.write(line)

    os.remove('employees.db')
    os.rename('employees.db_copy','employees.db')

    print("成功删除{0}员工信息!".format(user_id))

def main():
    while 1:
        print(menu)
        menu_select = input("请输入你要选的菜单: ").strip()       # .strip() 去除 空格 \t \n

        if menu_select == '1':      # input 获取的是 字符 1
            # 1、查看员工信息
            check_info()

        elif menu_select == '2':
            # 2、添加员工信息
            add_employees()

        elif menu_select == '3':
            # 3、修改员工信息
            upd_employees()

        elif menu_select == '4':
            # 4、删除员工信息
            del_employees()

        elif menu_select == '5':
            # 5、打印员工信息
            show_info()

        elif menu_select == '6':
            # 退出
            print("已退出程序,欢迎下次使用~ ")
            # exit()
            break
        else:
            print("您输入的内容有误,请重新输入!")

main()

发布了130 篇原创文章 · 获赞 283 · 访问量 24万+

猜你喜欢

转载自blog.csdn.net/Sunny_Future/article/details/103473544