Python Forum
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Try With
#1
The code below is an attempt to catch an error opening and parsing a JSON file. When an error occurs, it does not print the text in the except block. Can someone explain this?

Python 2.7 on raspberry Pi 3
on a Windows 10 system
using the Xming server so I can program with Geany on the Laptop

def read_schedule_file(self):
  try:
    with open("schedule.json") as sched:
      self.schedule_file = json.load(sched)
  except IOError as e:
    print 
    print("Something went WRONG")
    print 
    print "I/O error({0}): {1}".format(e.errno, e.strerror)
    print
  print("No Error")
I couldn't copy and paste the error from the Xmin server so here is an image
https://www.dropbox.com/s/dc3wthzsx8d9p3...r.JPG?dl=0
Reply
#2
You're trying to catch an IOError, but json is raising a ValueError. So your except block never runs, because it's waiting for a different kind of error.

Looks more like the file is opening just fine, there just isn't valid json in it.
Reply
#3
Yes, That is exactly what was wrong. Thank you.
Reply


Forum Jump:

User Panel Messages

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