# Json字符串進行新增操作
import json
import os
# os.path.dirname(__file__):表示當前目錄
path = os.path.join(os.path.dirname(os.path.dirname(__file__)),'data/login.json')
# 定義讀取文件方法
def read_a():
# 打開文件
with open(path,'r') as fp:
# 讀取返回文件
return fp.read()
# loads 反序列化,str裝換為python對象
temp = json.loads(read_a())
# 向Temp對象中新增對象
temp.update(
{'id':'123',"name":"ad"}
)
# dumps序列化:Python對象轉換為Json對象
new = json.dumps(temp)
# str數據追加
with open(path,'w+') as fp:
fp.write(new)