Python Forum

Full Version: Code doesn't seem to write anything
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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]
(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.
(May-11-2017, 06:48 AM)volcano63 Wrote: [ -> ]json.dumps dumps to a string; json.dump - to a file.

Thanks, learned something new today.
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.
Pages: 1 2