我想编写一个应该执行以下操作的代码:
- 将 JSON 数据写入文件
- 在异步 python 中运行以更新该文件
这是我到目前为止编写的代码:
import os
import json
data = [{"username": "name1", "email": "mail1", "password": "password1"},
{"username": "name2", "email": "mail2", "password": "password2"}]
def main():
a = int(input("What you want to do:\n1.Create\n2.Update\n3.Delete "))
if a == 1:
q = input("Enter the filename:")
create(q)
elif a == 2:
q = input("Enter the filename:")
update(q)
elif a == 3:
q = input("Enter the filename:")
delete(q)
else:
print("Enter the correct choice")
def create(q):
f = open(q, "w")
w = input("Enter the text:")
f.write(w)
f.close()
def update(q):
with open(q, 'a') as outfile:
json.dump(data, outfile, indent=4)
def delete(q):
os.remove(q)
main()