使用數(shù)據(jù)本地化 添加學(xué)生
import json
# 1.讀取普通文本文件內(nèi)容
def read_text_file(file: str):
"""
讀取指定文件中的內(nèi)容
:param file: 文件路徑
:return: 文件中的內(nèi)容
"""
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文件中的內(nèi)容
:param file: 文件地址
:return: 文件中數(shù)據(jù)對(duì)應(yīng)的python數(shù)據(jù)
"""
try:
with open(file, encoding='utf-8') as f:
return json.load(f)
except FileNotFoundError:
return None
# 將數(shù)據(jù)寫入json文件中
def write_json_file(file, obj):
"""
寫入數(shù)據(jù)到j(luò)son中
:param file:文件地址
:param obj:寫入數(shù)據(jù)(json數(shù)據(jù)才能寫入)
:return:返回寫入狀態(tài)
"""
try:
with open(file, 'w', encoding='utf-8') as f:
json.dump(obj, f, ensure_ascii=False)
return True
except:
return False