0

I am trying to create a python script to read a json report file (that contains status of tasks getting executed). so, when i run my script, i need to show the current status parsing the json report file and updating it in real time. i want it to run something like a linux 'top' command where the details get updated in real time until you choose to exit.

here is the code that i tried to come up with:

import json
import time

while True:
    with open('/tmp/install-report.json') as json_data:
        d = json.load(json_data)
        for key, value in d.iteritems():
            print key, value
        time.sleep(1)

so when i run it, the output keeps showing in a loop while messing up the console.

however, i need to show only the snapshot of the parsed json file. if there is any update in the json file, the output should show the updated information instead of running into a loop of output on the console.

please let me know how i can correct the above script to achieve what i want.

cool77
  • 1,086
  • 4
  • 15
  • 31
  • You should probably take a look at [inotify](https://github.com/seb-m/pyinotify/wiki) to react to changes to the file, and [curses](https://docs.python.org/2/howto/curses.html) if you're opposed to just printing the json data to the terminal. – Aran-Fey Aug 21 '16 at 09:17
  • Is [writing on the same line over and over](http://stackoverflow.com/q/517127/1377864) what you want? – Yaroslav Admin Aug 21 '16 at 09:36

0 Answers0