Update a file using async in Python

997 Views Asked by At

I want to write a code that should do the following:

  1. Write JSON data to a file
  2. Run in async python for updating that file

Here is the code that I've written till now:

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()
0

There are 0 best solutions below