menu

json保存配置文件

json保存配置文件


import json

def save(data):

with open('config.json', 'w') as fw:

# 将字典转化为字符串

# json_str = json.dumps(data)

# fw.write(json_str)

# 上面两句等同于下面这句

json.dump(data,fw)

# load json data from file

def load():

try:

with open('config.json','r') as f:

data = json.load(f)

return data

except:

with open('config.json', 'w') as fw:

json.dump({},fw)

return {}

  
  

CONF=load()

编辑