Python Forum
Code doesn't seem to write anything
Thread Rating:
  • 3 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code doesn't seem to write anything
#11
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]
Reply
#12
(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.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#13
(May-11-2017, 06:48 AM)volcano63 Wrote: json.dumps dumps to a string; json.dump - to a file.

Thanks, learned something new today.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#14
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  write code that resides in parent directory franklin97355 3 416 Apr-14-2024, 02:03 AM
Last Post: franklin97355
  Last record in file doesn't write to newline gonksoup 3 452 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  needing some help to write some code for a game calculator rymdaksel 1 422 Jan-02-2024, 09:56 AM
Last Post: deanhystad
  Code works but doesn't give the right results colin_dent 2 728 Jun-22-2023, 06:04 PM
Last Post: jefsummers
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,810 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  color code doesn't work harryvl 1 897 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  UART Serial Read & Write to MP3 Player Doesn't Work bill_z 15 5,853 Jul-17-2021, 04:19 PM
Last Post: bill_z
  My code doesn't work, can someone help me? aldasrasickas 5 2,825 Dec-21-2020, 02:26 PM
Last Post: aldasrasickas
  How to write a code with İF function? Aycaaxx 1 1,851 Nov-03-2020, 05:46 AM
Last Post: deanhystad
  Line of code to show dictionary doesn't work MaartenRo 2 2,452 Jul-28-2020, 03:58 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020