0

Let's say I have a json file with multiple values as such:

{
    "google":5,
    "apple":4,
    "msft":3,
    "amazon":6
}

Is there any way I can update each separate value in the file after a certain interval of time? For example, I can change the Google value from 5 to 6 then 7 and so on each minute.

Yash Sharma
  • 78
  • 1
  • 11
  • 2
    Why would this be different for JSON files from any other kind of file? What parts do you already know? Do you know how to write a JSON file _once_? If you do, do you know how to schedule a function to be called every 30 minutes? (If not, did you look for a preexisting Q&A entry describing how to do so?) – Charles Duffy Feb 13 '22 at 00:03
  • 1
    Ideally, the question wouldn't be about what you're trying to accomplish as an overarching goal, but about _where you got stuck_ doing it yourself. What's the narrow, specific technical problem that prevents you from implementing this on your own? – Charles Duffy Feb 13 '22 at 00:05
  • I guess it really wouldn't matter, but I'm specifically practicing using JSON files and I was wondering how I should proceed. In response to the first comment. – Yash Sharma Feb 13 '22 at 00:05
  • 4
    [call a function every x minutes](https://stackoverflow.com/questions/56570474/python-call-a-function-every-x-minutes); [how to write a new dictionary to a json file](https://stackoverflow.com/questions/68262954/how-to-write-a-new-dictionary-to-a-json-file) -- just two of the many relevant preexisting questions with answers. Try something yourself, using those and other existing questions' answers, and then you can ask a more specific question after you get stuck. – Charles Duffy Feb 13 '22 at 00:07
  • You are right. I should have broken down the problem. – Yash Sharma Feb 13 '22 at 00:08
  • 1
    Additionally to @CharlesDuffy there is also a [asynchronous](https://stackoverflow.com/questions/56729764/python-3-7-asyncio-sleep-and-time-sleep) way to do that, so you won't block your entire script with waiting – sudden_appearance Feb 13 '22 at 00:11

1 Answers1

1

With time.sleep() you can do this. time.sleep() delays your code for the amount of seconds that you specify, decimals are allowed. If you put it in a different thread, you can execute code whilst this is happening. This is some code for the threaded version, but you can remove threading by just calling updatedict()

import json
from time import sleep
import threading

values = {
    "google":5,
    "apple":4,
    "msft":3,
    "amazon":6
}
def updatedict():
    while True:
        global values
        #incrementing values
        for value in values:
            values[value] += 1
        #Writing file
        with open("values.json","w+") as f:
            f.write(json.dumps(values))
        #Sleeping 1 minute
        sleep(60)
#Starting new thread
threading.Thread(target=updatedict).start()
#Any code you wish to run at the same time as the above function

If you intend to run the script multiple times each incrementing onto what is already there, replace the existing values variable assignment with

try:
    with open("values.json") as f:
        values = json.load(f)
#If the file is not found, regenerates values
except FileNotFoundError:
    values = {
    "google":5,
    "apple":4,
    "msft":3,
    "amazon":6
}
Enderbyte09
  • 409
  • 1
  • 5
  • 11