Python Forum
Code doesn't seem to write anything - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Code doesn't seem to write anything (/thread-3274.html)

Pages: 1 2


RE: Code doesn't seem to write anything - Larz60+ - May-11-2017

Here's an example that writes a dictionary to json file.
   with open('data\RFCindex.json', 'w') as jout:
       json.dump(self.tdict, jout)
Build your dictionary completely, then write the whole thing
To read back in:
   with open('data\RFCindex.json', 'r') as jin:
       self.tdict = json.load(jin)
[/python]


RE: Code doesn't seem to write anything - volcano63 - May-11-2017

(May-10-2017, 11:46 PM)sparkz_alot Wrote: I've never used json, but a quick look at the docs, shouldn't it be  json.dumps rather than  json.dump ?
json.dumps dumps to a string; json.dump - to a file.


RE: Code doesn't seem to write anything - sparkz_alot - May-11-2017

(May-11-2017, 06:48 AM)volcano63 Wrote: json.dumps dumps to a string; json.dump - to a file.

Thanks, learned something new today.


RE: Code doesn't seem to write anything - PickyBiker - May-12-2017

Playing with it again this morning, and figured out the problem. I just moved the sleep down the file AFTER the try block.
It works now because the try doesn't close the file until the end of the try block so now it sleeps while the file is closed. 

If I stop the script at a random point, the status.json file now has whatever the count was up to when I stopped it.