1

I have to load a json file into a python script for part of my homework. Below is the part of the code which loads the JSON file:

import json
import GraphImplementation as G
from webbrowser import open
from ParseJson import parseJSONToGraph

def main():
    jsonFile = open("map_data.json")
    jsonData = json.load(jsonFile)
    graph = G.Graph()
    graph = parseJSONToGraph(graph, jsonData)

For some reason, on the json.load(jsonFile) line, the code fails with this error:

$ python main.py
Traceback (most recent call last):
  File "main.py", line 85, in <module>
    main()
  File "main.py", line 13, in main
    jsonData = json.load(jsonFile)
  File "/usr/lib/python2.7/json/__init__.py", line 286, in load
    return loads(fp.read(),
AttributeError: 'bool' object has no attribute 'read'

Does anyone know why this could be? In the interactive python mode I am able to run the load() call without any problem, I am not sure why it wouldnt work when I am executing my script.

2
  • Where did this line from webbrowser import open come from? Auto-import from some IDE? You're shadowing Python's open built-in, so your open("map_data.json") is failing. Commented Mar 11, 2015 at 22:47
  • I need the webbrowser open function later on in my code, that is why I have that line. I must have overlooked the fact that it is shadowing python's open. Thank you :) Commented Mar 11, 2015 at 23:05

1 Answer 1

3

webbrowser.open does not load a file from URL; instead it opens a web browser with that page, and returns True (a bool value), which passed to json.load causes an exception being thrown.

If you have a file that you want to open, just remove the from webbrowser import open line.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.