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
#8
Okay, thank you to all. Here is the corrected code with a call to main. The problem was I inadvertently left out the call to main when I posted my code, but it was actually in my code. I also remove the self as suggested.  

Now, why doesn't this code put data into the status.json file, but it does send the code to the terminal via the print command??

import json
import os
import time
import datetime

# write the status file data
def main():
       string = {"stat":[]}
       count = 0
       while True:
               try:
                       string["stat"] = count
                       with open("status.json", 'w') as f:
                               json.dump(string, f)
                               print(string)
                               count += 1
                               if count > 99:
                                       count = 0
                               time.sleep(2)
               except IOError as e:
                       print "I/O error({0}): {1}".format(e.errno, e.strerror)
               except ValueError:
                       print ("Can't write file status.json")

if __name__=="__main__":
       main()
Reply


Messages In This Thread
Code doesn't seem to write anything - by PickyBiker - May-10-2017, 06:24 PM
RE: Code doesn't seem to write anything - by nilamo - May-10-2017, 07:06 PM
RE: Code doesn't seem to write anything - by PickyBiker - May-10-2017, 11:17 PM

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

Forum Jump:

User Panel Messages

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