0

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()
Omar Einea
  • 2,478
  • 7
  • 23
  • 35
Rohit
  • 1
  • Welcome to StackOverflow! Unfortunately, the problem description *write json data to a file run in async python for updating that file* is very unclear. Have in mind that, while such a short description may be clear to *you*, it lacks details necessary for someone else to understand what the program needs to do. Can you please elaborate on the actual requirement? – user4815162342 Mar 31 '18 at 11:05
  • what i want to do is: if a file is being written/created, another file should be in process of updation simultaneously ... – Rohit Mar 31 '18 at 11:31
  • This update should be by the same program, or by another program? Is `main` supposed to be running a loop? – user4815162342 Mar 31 '18 at 12:09
  • the update should be by the same program.. main() is a call to the function. its not necessary to make main() to run in loop – Rohit Mar 31 '18 at 12:15
  • If there is no loop, then the program will exit after one operation? – user4815162342 Mar 31 '18 at 12:30
  • yes. what i want is as im typing text (when im in create funciton), the actual file must be updated in real time. Meaning i must be able to see the actual file being updated... – Rohit Mar 31 '18 at 12:34
  • *what i want is as im typing text (when im in create funciton), the actual file must be updated in real time.* That's not how `input` works - the function returns only after enter is pressed, and then you get the whole text. Even if you switched to asynchronous reading of standard input, the OS would still buffer the data until enter is pressed, and provide it all at once. If this is a learning exercise, it doesn't look like it is solvable as formulated. If not, then please state the **actual** problem you are trying to solve. – user4815162342 Mar 31 '18 at 12:47
  • yes its just a learning exercise... how about this:i go on entering data into the file(when im in update function ), and once i press enter the actual file should get updated using asyncio. And then the program should ask whether i want to type some more data . if yes start entering some data until i press enter and update again . this should go on and on... – Rohit Mar 31 '18 at 13:04
  • That makes sense, but unfortunately it's not easy to achieve. stdin is blocking, so reading it asynchronously requires non-portable code provided by a third-party package such as [aioconsole](https://github.com/vxgmichel/aioconsole). Asynchronous io for ordinary files is again highly non-portable (most code simply uses blocking code and hope they won't block for _too_ long), so asyncio doesn't do it out of the box, you again need an external package like [`aiofiles`](https://pypi.python.org/pypi/aiofiles). Either operation is simply not as needed in typical asyncio program as you'd think. – user4815162342 Mar 31 '18 at 14:48
  • Paradoxically, changing the problem statement to something that looks harder, say reading from and writing to the network, makes it much easier to implement with pure asyncio. – user4815162342 Mar 31 '18 at 14:50

0 Answers0