4

Here is my code:

import json
from difflib import get_close_matches
data = json.load(open("data.json")) # I get an error here
def translate(w):
    w = w.lower()
    if w in data:
        return data[w]
        elif len(get_close_matches(w, data.keys())) > 0:
        yn = input("Did you mean %s instead? Enter Y if yes, or N if no: " % get_close_matches(w, data.keys())[0])
        if yn == "Y":
            return data[get_close_matches(w, data.keys())[0]]
        elif yn == "N":
            return "The word doesn't exist. Please double check it."
        else:
            return "We didn't understand your entry."
    else:
        return "The word doesn't exist. Please double check it."

word = input("Enter word: ")
output = translate(word)
if type(output) == list:
    for item in output:
        print(item)
else:
    print(output)

On this line: data = json.load(open("data.json")), I get the following error:

IOError: [Errno 2] No such file or directory: 'data.json'

How can I fix this error?

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
Ragulan V
  • 49
  • 1
  • 1
  • 2
  • 2
    You need to mention the physical location of your json file in data = json.load(open("full path to your json")) – Rakesh Jan 20 '18 at 14:29
  • Can you give us the error message please? – costrouc Jan 20 '18 at 14:37
  • 2
    The error message tells you there is no file named "data.json" in your current directory(where you launch your python). – llllllllll Jan 20 '18 at 14:39
  • @liliscent tempting though it is to steal your thunder- I suggest you post that as an answer and include the fact that if the user running the program doesn't have permissions to read data.json, that they may see the same error. – Philip Adler Jan 20 '18 at 15:35
  • 1
    @PhilipAdler Maybe you can add an answer. But as I know, if you don't have permission to read that file, the error should be "Permission denied", not "No such file". – llllllllll Jan 20 '18 at 15:47

1 Answers1

1

As the error message suggest, there is no file called "data.json" in the same directory as the one in which you are running the program. If you explicitly use the full path to the file, the program should work.

Philip Adler
  • 2,111
  • 17
  • 25