day12作业

使用数据本地化 添加学生

import json 


# 1.读取普通文本文件内容
def read_text_file(file: str):
    """
    读取指定文件中的内容
    :param file: 文件路径
    :return: 文件中的内容
    """
    try:
        with open(file, encoding='utf-8') as f:
            return f.read()
    except FileNotFoundError:
        return None


# 2. json文件的读
def read_json_file(file: str):
    """
    读取指定json文件中的内容
    :param file: 文件地址
    :return: 文件中数据对应的python数据
    """
    try:
        with open(file, encoding='utf-8') as f:
            return json.load(f)
    except FileNotFoundError:
        return None


# 将数据写入json文件中
def write_json_file(file, obj):
    """
    写入数据到json中
    :param file:文件地址
    :param obj:写入数据(json数据才能写入)
    :return:返回写入状态
    """
    try:
        with open(file, 'w', encoding='utf-8') as f:
            json.dump(obj, f, ensure_ascii=False)
            return True
    except:
        return False

你可能感兴趣的:(day12作业)