首页 > 编程语言 >用Python实现一个基于文件存储的控制台学生管理系统

用Python实现一个基于文件存储的控制台学生管理系统

时间:2023-01-06 14:57:49浏览次数:45  
标签:student 管理系统 Python json file print path 控制台 os

放假回家,写写Python玩,10分钟写了一个基于文件存储的控制台学生管理系统,目的是熟悉Json的使用

import json
import os
import atexit
import time

latest_file = ""

def main():
    read_now_time() #程序刚启动时读取上次保存的时间戳文件
    while(True):
        print_menu()
        choose = int(input("输入您的选项:"))
        if choose==1:
            add_stu()
        elif choose==2:
            delete_stu()
        elif choose==3:
            update_stu()
        elif choose==4:
            display_stu()
        else:
            exit()

def print_menu():
    print ("====================================================")
    print ("1.添加学生信息 2.删除学生信息 3.更新学生信息 4.显示学生信息 5.退出")
    print ("====================================================")

def add_stu():
    id = input("输入学生证件号:")
    name = input("输入学生姓名:")
    grade = input("输入学生成绩:")
    student = {"id": id, "name": name, "grade": grade}
    students = read_json(from_add=True)
    if not students:
        students = [student]
    else:
        students.append(student)
    write_json(students)

def delete_stu():
    students = read_json()
    id = input("输入要删除学生的证件号:")
    find_id = False
    for student in students:
        if id == student['id']:
            find_id = True
            print ("学生号:" + id + ",姓名:" + student['name'], ",成绩:" + student['grade'])
            sure = input("确认要删除该学生信息吗:Y/N")
            if sure=="Y":
                students.remove(student)
                write_json(students)
            break
    if not find_id:
        print("未找到该学生")

def update_stu():
    print("待定......")

def display_stu():
    students = read_json()
    for student in students:
        print ("----->" + "学生证件号:" + student['id'] + ",学生姓名:" + student['name'] + ",学生成绩:" + student['grade'])

def read_json(from_add=False):
    if not latest_file:
        if not from_add:
            print ("请先添加学生")
        return None
    file_dir = os.path.join(os.getcwd(), "json")
    file_path = os.path.join(file_dir, latest_file)
    with open(file_path, "r+", encoding="utf-8") as fp:
        json_dict = json.load(fp)
        return json_dict

def write_json(json_dict):
    global latest_file
    file_dir = os.path.join(os.getcwd(), "json")
    if not os.path.exists(file_dir):
        os.mkdir(file_dir)
    file_name = str(int(time.time()))
    latest_file = file_name
    file_path = os.path.join(file_dir, file_name)
    json_data = json.dumps(json_dict)
    with open(file_path, "w+") as fp:
        fp.write(json_data)

def read_now_time():
    global latest_file
    now_time_file = os.path.join(os.getcwd(), "now")
    if not os.path.exists(now_time_file):
        print ("时间戳文件不存在,无法定位上次保存的学生数据,重新开始保存数据")
        return
    with open(now_time_file, "r+") as fp:
        latest_file = fp.readline().strip()
        if not latest_file:
            print("时间戳文件为空,无法定位上次保存的学生数据,重新开始保存数据")
        else:
            print("时间戳文件为:" + latest_file + ",加载上次保存的数据......")

#程序退出时,自动保存当前时间戳
@atexit.register
def write_now_time():
    if not latest_file:
        return
    now_time_file = os.path.join(os.getcwd(), "now")
    with open(now_time_file, "w+") as fp:
        fp.write(latest_file)

if __name__ == '__main__':
    main()

标签:student,管理系统,Python,json,file,print,path,控制台,os
From: https://www.cnblogs.com/z5onk0/p/17030462.html

相关文章

  • Python 迭代器Iterator详情
    1.什么是迭代器?迭代器是一个表示数据流的对象,当我们调用next()方法时会返回容器中的下一个值迭代器中包含__iter__和__next__()方法。通过__iter__方法可以返回迭代器......
  • 推送自研包到python仓库
    环境Python版本:3.6.8仓库:JFrogArtifactory步骤包制作https://zhuanlan.zhihu.com/p/37987613.pypirc文件在$home目录下创建.pypirc文件从Artifactory平......
  • 聊天尬住了?教你用Python一键获取斗图表情包,从此摇身变海王
    很多兄弟在聊天上没有下太多的功夫,导致自己聊天的时候很容易尬住,然后就不知道聊啥了,这时候合适表情包分分钟就能救场,但是一看自己收藏的表情包,好家伙,两只手都数得过来。......
  • python 读取excel表格中的数据
    有如下一张存储了数据的excel表,如下图所示,想要通过python代码将其中的数据提取出来方法步骤1、确定excel表格存放路径,这里以我的为例:/Users/Desktop/honops/USERRES/app......
  • Python学习day03
    一、内容回顾1.多行注释也等于多行打印python中单引号与双引号是一样的msg='''hello1hello2hello3'''print(msg)#结果:#hello1#hello2#hello31.for、if、while(br......
  • C语言图书管理系统[2023-01-06]
    C语言图书管理系统[2023-01-06]模仿图书馆的借书还书操作,用C语言实现图书管理系统。系统必须先登录方可进入系统。该系统分为读者和图书管理员2类用户,若是读者登录成功后......
  • python -m json.tool 格式化json 中文转码
     使用参数:--no-ensure-ascii  catjob_config.json  |python-mjson.tool --no-ensure-ascii  > job_config_format.json  具体查询本机:/usr/XXXXX......
  • 常见大O运行时间·Python例子
    常见大O运行时间·Python例子O(1)O(1)描述了一种无论输入数据集的大小如何总是在相同的时间(或空间)内运行的算法。defis_first_element_null(order_list):return......
  • 「docker实战篇」python的docker爬虫技术-移动自动化控制工具appium工具(17)
    框架,可用于原生,混合和移动Web应用程序测试。它使用WebDriver协议驱动iOS,Android应用程序。直接多种语言:java,python等等。appium架构####desiredcapabilitydesiredcapabil......
  • 「docker实战篇」python的docker爬虫技术-移动自动化控制工具安卓ADB的使用(15)
    ebugBridge),安卓平台调试桥,是连接Android手机与PC端的桥梁,通过adb可以管理、操作模拟器和设备,如安装软件、查看设备软硬件参数、系统升级、运行shell命令等。####adb首先需......